Ruby on rails Rails路由,是否有许多可选的嵌套资源健全性?
我已经知道如何在routes文件中设置嵌套资源了……问题是如何选择使用相同的负载和更少的行进行设置 假设我有一个Ruby on rails Rails路由,是否有许多可选的嵌套资源健全性?,ruby-on-rails,ruby,ruby-on-rails-3,routing,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Routing,Ruby On Rails 3.2,我已经知道如何在routes文件中设置嵌套资源了……问题是如何选择使用相同的负载和更少的行进行设置 假设我有一个博客站点。BlogSite有许多帖子,但也有许多作者和许多日期。(这可能不是最好的例子,但请容忍我) 要在帖子上执行CRUD,我希望能够使用 /blog_sites/1/author/2/date/3/posts #all posts on site 1 from author 2 on date 3 /blog_sites/1/author/2/posts #all p
博客站点
。BlogSite有许多帖子
,但也有许多作者
和许多日期
。(这可能不是最好的例子,但请容忍我)
要在帖子上执行CRUD,我希望能够使用
/blog_sites/1/author/2/date/3/posts #all posts on site 1 from author 2 on date 3
/blog_sites/1/author/2/posts #all posts on site 1 from author 2
/blog_sites/1/date/3/posts #all posts on site 1 on date 3
/blog_sites/1/posts #all posts on site 1
/author/2/date/3/posts #all posts from author 2 on date 3
/author/2/posts #all posts from author 2
/date/3/posts #all posts from date 3
/posts #all posts
这样,URL中的任何过滤参数都可以是可选的。我知道你可以用
get (/blog_sites/:blog_id)(/author/:author_id)(/date/:date_id)/posts => "posts#index"
但我不想失去使用嵌套资源路由的所有CRUD好处。目前,我必须复制大部分路由以使其正常工作,我正在寻找更好的方法:
resources :blog_sites do
resources :authors do
resources :dates do
resources :posts
end
resources :posts
end
resources :dates do
resources :posts
end
resources :posts
end
……等等。它很快就会变得难以管理
如何在保持routes的同时维护可选的参数URL。rbDRY
和sane?尝试一起使用范围和资源
这正是我最终解决它的方式,尽管我使用了嵌套的作用域,这样我就可以在每一行的资源旁边清晰地指定所有参数的条件
scope 'blog_sites/:blog_id)(/author/:author_id)(/date/:date_id)' do
resources :posts
end