Rails 命名空间控制器问题
我已将 Rails 应用程序分为管理目录和公共目录。
admin 目录位于 Admin:: 命名空间 (/admin) 内。
当我在 Admin:: 命名空间和普通根映射区域中创建一个名为 Forums 的控制器时,路由似乎找到了 /forums 和 /admin/forums 的 Admin::Forums 控制器。
所以 /admin/forums => “应用程序/控制器/管理/forums_controller.rb” 所以 /forums => “app/controllers/admin/forums_controller.rb”
不确定为什么会发生这种情况,根控制器是否以某种方式在两个控制器之间继承?当我尝试在非管理论坛控制器中执行代码时,没有执行任何操作。
这是我的路线:
map.resources :forums, :only => [:index,:show] do |forum|
forum.resources :topics, :shallow => true, :only => [:index,:show], :name_prefix => ""
end
map.namespace :admin, :name_prefix => "", :path_prefix => "/admin", :name_prefix => "admin_" do |admin|
admin.resources :forums, :name_prefix => 'admin_' do |forum|
forum.resources :topics, :name_prefix => 'admin_' do |topic|
topic.resources :posts, :name_prefix => 'admin_'
end
end
end
有什么想法吗?
I've split my rails app into a admin directory and a public directory.
The admin directory is within an Admin:: namespace (/admin).
When I created a controller called Forums in both the Admin:: namespace and the normal root map area, the routing seems to find the Admin::Forums controller for /forums and /admin/forums.
So /admin/forums => "app/controllers/admin/forums_controller.rb"
So /forums => "app/controllers/admin/forums_controller.rb"
Not sure why this is happening, are the root controllers somehow inherited amoung both controllers? When I try to execute code within the non-admin forums controller, nothing gets exercuted.
Here is my route:
map.resources :forums, :only => [:index,:show] do |forum|
forum.resources :topics, :shallow => true, :only => [:index,:show], :name_prefix => ""
end
map.namespace :admin, :name_prefix => "", :path_prefix => "/admin", :name_prefix => "admin_" do |admin|
admin.resources :forums, :name_prefix => 'admin_' do |forum|
forum.resources :topics, :name_prefix => 'admin_' do |topic|
topic.resources :posts, :name_prefix => 'admin_'
end
end
end
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为主管理命名空间调用指定 name_prefix 两次(这实际上是随机选择一个选项)。此外,您不需要子资源中的 name_prefix 选项。这是我的应用程序中的 - 命名空间中的一些子资源(问题和用户)也是主要资源,不会造成混淆。
You specify the name_prefix twice for the main admin namespace call (which is going to essentially choose one option at random). Also you don't need the name_prefix option in the sub resources. Here's mine from my app - some of the sub resources in the namespace (questions and users) are also main resources and there's no confusion.