0

我有一个名为 的模型Contributor,它也充当其他几个模型的命名空间,例如Contributor::AliasContributor::Reassignment。我想使用包含贡献者 ID 的 URL,如下所示:

/contributors/1/reassignments/new

但我得到这个错误:

No route matches [GET] "/contributors/1/reassignments/new"

我的routes.rb文件包括:

namespace :contributor do
  resources :reassignments
end
resources :contributors

我也试过:

resources :contributors do
  resources :reassignments
end

这会导致不同的错误:

uninitialized constant ReassignmentsController

知道如何解决这个问题吗?也许我不应该使用同时充当模型的命名空间?我没有在任何教程中看到这样做,尽管它似乎是可能的。

更新:

如何处理深度嵌套的命名空间模型,例如:

resources :contributors do
  resources :reassignments, :module => "contributor" do
    resources :approvals, :module => "reassignment"
  end
end

使用这种方法,我得到了错误:

No route matches {:action=>"create", :controller=>"contributor/reassignment/approvals"}

我的控制器目录确实具有以下结构:

contributor ->
  reassignment ->
    approvals_controller.rb

这似乎与第一个错误有关,但也许是新事物。

4

1 回答 1

1

目前尚不清楚您是否有贡献者资源。如果这样做,请在您的 routes.rb 中添加以下内容:

resources :contributors do
  resources :reassignments, :module => "contributor"
end

如果没有,请尝试:

resources :reassignments, :module => "contributor", :path => "/contributors/:contributor_id/reassignments"

请注意,在第二种情况下,您将需要构造一个 url 并在对 link_to、form_for 和类似位置的调用中明确地将 :contributor_id 传递给它。

如果你想在那里使用 [@contributor,@reassignment] 格式,你最好坚持第一种方法,你确实有一个贡献者资源。

更新:对于三级嵌套,如果您的控制器目录不与资源并行嵌套,您可以显式指定控制器,例如:

resources :contributors do
  resources :reassignments, :controller => "contributor/reassignments" do
    resources :approvals, :controller => "reassignment/approvals"
  end
end  

但是,请不要那样做。在 Rails 中不鼓励使用 3 层及更多层的嵌套。在这里查看推荐的内容:http ://weblog.jamisbuck.org/2007/2/5/nesting-resources

于 2013-02-28T02:09:13.467 回答