I'm using Rails 3 and I have two models EquipmentGroup and Reservation. I want reservations to be a nested resource of equipment groups so that I can access them with URLs like:
/equipment_groups/:equipment_group_id/reservations/:id
However, I don't want to create routes for the equipment groups. I can achieve this through the following, but it seems like a hack:
resources :equipment_groups, :only => [] do resources :reservations end
Is there a better way to do this? I can't seem to find an answer easily in the documentation.
Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.
In a nested route, the belongs_to (child) association is always nested under the has_many (parent) association. The first step is to add the nested routes like so: In the above Rails router example, the 'index' and 'show' routes are the only nested routes listed (additional or other resources can be added).
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.
Your approach - it's a standard approach, there is nothing better.
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