Pet*_*kou 9 ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-4
我正在命名空间"admin"中创建自定义管理面板.
我在该命名空间中有资源"课程".
但我还想要一个不在该命名空间中的"课程"的路线:
例如:BOTH localhost:3000/admin/coursesANDlocalhost:3000/courses
如果这需要不同的控制器,那就没关系.
我担心的是,如果我有相同路线的两种资源,它就不会真的干.
namespace admin do
resources :courses
end
Run Code Online (Sandbox Code Playgroud)
只是
resources :courses
Run Code Online (Sandbox Code Playgroud)
有没有办法在命名空间和没有命名空间之间共享一个资源,或者是上面的示例?
我不太确定我明白你的意思,但是
namespace :something 实际上是一个简写 scope :something, module: :something, as: :something
scope :something将添加/something/为URL前缀scope module: :something将添加/something为控制器前缀(控制器将在下面获取controlelrs/something/the_controller.rbscope as: :something将添加something路径助手的前缀现在,在您的路线中同时使用它们是完全没问题的
resources :courses
# Will generate "/courses/", "/courses/new", "/courses/1/edit", ...
# And will point to `controllers/courses_controller.rb`
namespace :admin do
resources :courses
end
# Will generate "/admin/courses/", "/admin/courses/new", "/admin/courses/1/edit", ...
# And will point to `controllers/admin/courses_controller.rb`
Run Code Online (Sandbox Code Playgroud)
这回答了你的问题了吗 ?
等一下 !还有可能使用顾虑!
concern :shared_actions do
resources :courses
resources :something_else
end
namespace :admin do
concerns :shared_actions
end
concerns :shared_actions # Will add it to the root namespace ^^
Run Code Online (Sandbox Code Playgroud)
编辑:显然这是这个家伙也试图做的事情:D