Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 routing: Avoiding Deep Nesting

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!

like image 364
Ant Avatar asked Jun 14 '11 14:06

Ant


People also ask

What is shallow nesting?

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.

What are the 7 CRUD routes generated by the resources Method resources songs in Rails?

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.

What is RESTful route in Rails?

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.

What is concern in Rails routes?

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.


1 Answers

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.

like image 98
Christian Fazzini Avatar answered Sep 28 '22 02:09

Christian Fazzini