Question

I have Vehicle and STI models Car and Motorcycle.

I have currently routes configured as below:

resources :vehicles
resources :cars
resources :motorcycles

However, I'm thinking it would look prettier like this:

/vehicles/ #all the methods of vehicles_controller
/vehicles/cars/ #all the methods of cars_controller
/vehicles/motorcycles/ #all the methods of motorcycles_controller

How should I go about configuring it to get that URL?

Was it helpful?

Solution

I think you can do following:

resources :vehicles
scope "/vehicles" do
  resources :cars, :motorcycles
end

Or this way:

resources :vehicles
resources :cars, :path => "/vehicles/cars"
resources :motorcycles, :path => "/vehicles/motorcycles"

You can find more information about rails routing here: http://guides.rubyonrails.org/routing.html

OTHER TIPS

You can write :

resources :vehicles do
  resources :cars
  resources :motorcycles
end

You can write nested routes like this:

resources :vehicles do
  resources :cars
  resources :motorcycles
end

This will give you routes like:

/vehicles #index action for vehicles controller
/vehicles/:id #show action for vehicles controller
/vehicles/:vehicle_id/cars #index action for cars controller, with the :vehicle_id param available
/vehicles/:vehicle_id/cars/:id #show action for cars controller, with the :vehicle_id param available
etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top