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!
Shallow nesting Adding the 'shallow: true' parameter to your nested resource will define routes at the base level, '/songs/', for four of our RESTful routes — show, edit, update and destroy, while leaving the remaining routes — index, new, create— at the nested level.
In Rails, there are seven standard CRUD actions: index, show, new, create, edit, update, and destroy, which relate to specific HTTP verbs and are usually implemented using specific ActiveRecord methods.
In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.
Concerns can be defined in Rails routes to be able to have reusable routes. Sometimes, we need similar routes for different resources. Let's take an example. Let's say, we have a User and Post entity.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With