2013-02-28 64 views
0

我有一个名为Contributor的模型,它也充当其他几个模型的名称空间,例如Contributor::AliasContributor::Reassignment。我想使用包括贡献者ID像这样一个URL:Rails 3:获取名称空间模型的ID

/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 

任何想法如何处理这个?也许我不应该使用也作为模型的命名空间?我没有在任何教程中看到过这一点,尽管看起来这可能是可能的。

UPDATE:

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

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 

这似乎与第一个错误有关,但也许它是新东西。

回答

1

目前尚不清楚您是否有Contributor资源。如果你这样做,在你的routes.rb如下:

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

如果没有,请尝试:

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

只要注意的是,在第二种情况,你需要构建一个URL,然后明确传递:contributor_id在调用link_to,form_for和类似的地方。

如果你想使用[@ contributor,@ reassignment]格式,那么你最好坚持第一种方法,其中yu有一个Contributor资源。

UPDATE:为三级嵌套,如果你的控制器的目录不也是窝在并行,你可以明确指定控制器的资源,例如:

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

+0

非常好!我对模块路由不熟悉,但它在这里做了诀窍。如果你有时间,我有一个相关的问题。我有另一个命名空间/模型嵌套在第二层,我有类似的路由问题。我已经更新了我的问题,如果你有机会,你会看看吗? – nullnullnull 2013-02-28 03:01:32

+0

您的控制器在approvals_controller.rb中如何定义?是参与者::重新分配:: ApprovalsController还是smth其他?这可能是原因。 – moonfly 2013-02-28 03:23:11

+0

它是'Contributor :: Reassignment :: ApprovalsController'。更新后仍然没有运气,但我会继续尝试,并让我知道如果我找到解决方案。如果您有任何其他建议,那也将不胜感激。 – nullnullnull 2013-02-28 03:30:46