【发布时间】:2017-02-27 03:54:56
【问题描述】:
我正在尝试学习命名空间。
我之前就这个话题问过几个问题,但我不明白发生了什么。
我在控制器的文件夹中创建了一个名为“功能”的文件夹。在其中,我保存了一个名为 app_roles_controller.rb 的文件。
该控制器的第一行是:
class Features::AppRolesController < ApplicationController
功能文件夹的目的是让我可以更好地组织我的文件(就是这样)。
在我的 routes.rb 中,我尝试过:
resources :app_roles, :namespace => "features", :controller => "app_roles"
我也试过了:
namespace :features do
resources :app_roles
end
我有一个名为 app_role.rb 的模型(顶级),我有一个保存为 views/features/app_roles 的视图文件夹,其中包含索引、显示等文件。我的架构中的表称为“app_roles”。
当我为 app_roles 搜索路由时,我得到:
Paths Containing (app_role):
app_roles_path GET /app_roles(.:format)
app_roles#index {:namespace=>"features"}
POST /app_roles(.:format)
app_roles#create {:namespace=>"features"}
new_app_role_path GET /app_roles/new(.:format)
app_roles#new {:namespace=>"features"}
edit_app_role_path GET /app_roles/:id/edit(.:format)
app_roles#edit {:namespace=>"features"}
app_role_path GET /app_roles/:id(.:format)
app_roles#show {:namespace=>"features"}
PATCH /app_roles/:id(.:format)
app_roles#update {:namespace=>"features"}
PUT /app_roles/:id(.:format)
app_roles#update {:namespace=>"features"}
DELETE /app_roles/:id(.:format)
app_roles#destroy {:namespace=>"features"}
我不明白我做错了什么。
当我尝试时:
http://localhost:3000/app_roles#index
我收到一条错误消息:
uninitialized constant AppRolesController
当我尝试时:
http://localhost:3000/features/app_roles#index
我收到一条错误消息:
No route matches [GET] "/features/app_roles"
我正在寻找有关如何设置的简单英语解释。我已经尝试过编程红宝石书(几次)。
请您帮我了解在我的 rails 应用程序中引入组织文件需要发生什么?
【问题讨论】:
-
你真的触发了这个网址吗? `localhost:3000/features/app_roles#index
? Remove the#index` 在您的网址中。 -
@araratan - 这也不起作用。当我尝试:localhost:3000/features/app_roles 时,我收到一条错误消息:No route matches [GET] "/features/app_roles"
-
您是否阅读过有关布线的 Rails 指南?它通常是一个值得一看的好地方。关于命名空间的部分在这里:guides.rubyonrails.org/… 它建议您尝试类似:
resources :app_roles, module: 'features'这将导致索引页面的路径类似于features_app_roles_path(但我建议阅读该指南以了解更多关于它的工作原理和期望它做什么) -
是的 - 我已经读过了。我找不到任何没有行话的东西。我对这些资源感到很困惑。无论如何,谢谢 - 我会继续努力。理解这一点可能离我太远了。也许将所有内容都保持在顶层杂乱无章,这比弄清楚如何将编码指南翻译成英文更容易。
标签: ruby-on-rails ruby routes namespaces