Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nested routes Rails 3 without actually allowing access to the parent resource

Tags:

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.

like image 385
Courtney Avatar asked Mar 15 '11 18:03

Courtney


People also ask

What is the difference between resources and resource in Rails?

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.

What are nested routes in Rails?

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).

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.


1 Answers

Your approach - it's a standard approach, there is nothing better.

like image 91
kaleb4eg Avatar answered Sep 22 '22 20:09

kaleb4eg