Courtney
Courtney

Reputation: 241

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

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.

Upvotes: 22

Views: 2360

Answers (2)

kaleb4eg
kaleb4eg

Reputation: 2564

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

Upvotes: 9

Logan Leger
Logan Leger

Reputation: 662

I can think of a few ways of doing this. One way is what you've done above. However, it seems like you have no need to expose the equipment groups controller or any of its actions, so the following should do just fine:

scope "/equipment_groups" do
    resources :reservations
end

The scope block will append "/equipment_groups" to every route in it. This will essentially "fake" a nested route.

Upvotes: 0

Related Questions