Rails 命名空间控制器问题

发布于 2024-09-07 18:52:49 字数 954 浏览 6 评论 0原文

我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-09-14 18:52:49

您为主管理命名空间调用指定 name_prefix 两次(这实际上是随机选择一个选项)。此外,您不需要子资源中的 name_prefix 选项。这是我的应用程序中的 - 命名空间中的一些子资源(问题和用户)也是主要资源,不会造成混淆。

  map.namespace :admin do |admin| 
    admin.resources :home, :only => [:index]
    admin.resources :questions, :collection => {:edit_by_text => :get, :update_by_text => :post, :import_progress => :post}
    admin.resources :users
    admin.resources :subjects, :member => {:make_quizzes => :post}
  end

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.

  map.namespace :admin do |admin| 
    admin.resources :home, :only => [:index]
    admin.resources :questions, :collection => {:edit_by_text => :get, :update_by_text => :post, :import_progress => :post}
    admin.resources :users
    admin.resources :subjects, :member => {:make_quizzes => :post}
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文