I have these models:
Jump to content
The ultimate community for Ruby on Rails developers.
Posted 02 September 2013 - 09:34 PM
I have these models:
Posted 02 September 2013 - 10:46 PM
There are many ways to achieve this but possibly the simples for you would be to use nested routes
A brief excerpt from the rails guides http://guides.rubyon...rg/routing.html
2.7.1 Limits to Nesting
You can nest resources within other nested resources if you like. For example:resources :publishers doresources :magazines doresources :photosendend
Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as:/publishers/1/magazines/2/photos/3
The corresponding route helper would be publisher_magazine_photo_url, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular article by Jamis Buck proposes a rule of thumb for good Rails design:2.7.2 Shallow Nesting
Resources should never be nested more than 1 level deep.
One way to avoid deep nesting (as recommended above) is to generate the collection actions scoped under the parent, so as to get a sense of the hierarchy, but to not nest the member actions. In other words, to only build routes with the minimal amount of information to uniquely identify the resource, like this:resources :posts doresources :comments, only: [:index, :new, :create]endresources :comments, only: [:show, :edit, :update, :destroy]
This idea strikes a balance between descriptive routes and deep nesting. There exists shorthand syntax to achieve just that, via the :shallow option:resources :posts doresources :comments, shallow: trueend
This will generate the exact same routes as the first example. You can also specify the :shallow option in the parent resource, in which case all of the nested resources will be shallow:resources :posts, shallow: true doresources :commentsresources :quotesresources :draftsend
The shallow method of the DSL creates a scope inside of which every nesting is shallow. This generates the same routes as the previous example:
Programming is just about problem solving!
0 members, 0 guests, 0 anonymous users