Rails 3 routing: Avoiding Deep Nesting

别等时光非礼了梦想. 提交于 2019-12-01 01:14:27

问题


Today I realised I'd gotten a little carried away with nested resources:

resources :organisations do
  resources :studies do
    resources :settings
  end
end

The Rails guidelines (and my own thoughts) suggest that you shouldn't nest more than 1 level deep, so I refactored to this:

resources :organisations do
  resources :studies
end
resources :studies do
  resources :settings
end

Does anyone know a cleaner / more concise way to declare the above routes? Google gave me a lot of Rails 2-specific stuff.

Many thanks!


回答1:


You pretty much got it figured out and on the right track. It really depends on your domain. Just looking at your routes, I would ponder on what Settings does. Maybe a namespace somewhere to handle settings would suffice, maybe not. Really depends on what you are trying to do.

However, as far as nesting goes. It's looking fine.

PS. You can also refer to this guide for routing in Rails 3.0.X.



来源:https://stackoverflow.com/questions/6345314/rails-3-routing-avoiding-deep-nesting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!