ere
ere

Reputation: 1779

short nested rails routes

I have routes such as:

somedomain.com/cities/prague/places/astronomical-clock

  resources :cities do
    resources :places
    resources :images
  end

I know I can do something like:

  match '/:id' => 'cities#show', :as => 'short_city' # /prague

but what can I do about having short nested routes?

ie. /prague/astronomical-clock?

And could/should I make where it overwrites the default url_for methods?

Upvotes: 0

Views: 170

Answers (2)

nickaknudson
nickaknudson

Reputation: 4807

You'll have a slight issue differentiating between places and images, but this should work without any controller modification (assuming your above nested routes work)

match ':city_id/:id' => 'places#show'

OR

match ':city_id/:id' => 'images#show'

to differentiate between then I would recommend

match ':city_id/places/:id' => 'places#show'
match ':city_id/images/:id' => 'images#show'

OR dynamically assign the controller

match ':city_id/:controller/:id', :action => :show

RESOURCES

Upvotes: 1

holden
holden

Reputation: 13581

I would agree with Nick's answer but add that if you want to supersede the url_for method you can use :as

match '/:id' => 'cities#show', :as => 'city'
match ':city_id/:id' => 'places#show', :as => 'city_place'

This way it overwrites the usual city_path url_for methods.

Upvotes: 1

Related Questions