简体   繁体   English

Rails3路由:具有名称空间的资源

[英]Rails3 routing: Resource with namespace

Is it possible to define a namespace parameter when defining my routes, like this: 定义我的路线时是否可以定义名称空间参数,如下所示:

resource :account, :namespace => :account do
    resources :comments
end

So /account/comment calls Account::CommentsController instead of just CommentsController . 因此, /account/comment调用Account::CommentsController而不是CommentsController Just as a note, the :namespace -option above does NOT seem to work. 请注意,上面的:namespace -option似乎不起作用。

I can of course just add :controller => 'account/comments' to the comments resources, but having a lot of nested resources, this is not very DRY. 我当然可以在评论资源中添加:controller => 'account/comments' ,但是嵌套资源很多,这不是很干。

Is there better way? 有更好的办法吗?

Thanks guys - you rock! 谢谢大家-您真棒!

Okay, after some digging around I seem to have found a solution: 好吧,经过一番挖掘之后,我似乎找到了一个解决方案:

resource :account, :controller => 'account' do
    scope :module => 'account' do
        resources :comments
        ...
    end
end

This results in: 结果是:

/account/comments being linked to Account::CommentsController while still being able to acccess the regular CRUD-methods using /account /account/comments链接到Account::CommentsController同时仍然能够使用/account访问常规的CRUD方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM