如何在命名空间和根路径的路由中有一个资源 – Rails 4
我正在命名空间“admin”中创建自定义管理面板。
我在该命名空间中有资源“课程”。
但我还想要一个不在该命名空间中的“课程”的路线:
例如:BOTH localhost:3000/admin/courses
AND localhost:3000/courses
如果这需要不同的控制器,那没关系。
我担心的是,如果我有相同路线的两种资源,它就不会真的干。
namespace admin do resources :courses end
只是
resources :courses
有没有办法在命名空间和没有命名空间之间共享一个资源,或者是上面的示例?
等一下 ! 还有可能使用顾虑 !
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 ^^
编辑:显然这是这个家伙也试图做的事情:D
我不太确定我明白你的意思,但是
namespace :something
实际上是scope :something, module: :something, as: :something
的简写scope :something, module: :something, as: :something
-
scope :something
会添加/something/
作为URL前缀 -
scope module: :something
会添加/something
作为控制器前缀(控制器将在controlelrs/something/the_controller.rb
下获取 -
scope 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`
这回答了你的问题了吗 ?