Rails 4 nested routes

routes.rb

Rails.application.routes.draw do
  root to: 'visitors#index'

  resources :states do
    resources :cities do
      get 'listings'
    end
  end

end

      

I want to set up my GET url as:  ../state.id/city.id/listings.id

I am using friendly_id

, so urls will read:

../OR/Portland/2011-ford-truck

      

+3


source to share


1 answer


In this case, the listing is also its own model (resource). You will also need resources

for a printout. If it only has an action show

, you can restrict it like this:



resources :states do
  resources :cities do
    resources :listings, only: [:show]
  end
end

      

+3


source







All Articles