1

I'm trying to make some nice simple routes in Rails 3 with 2 custom matchers which assume the root :id will be a city and :city_id/:id will be a place... it seems to work fine except when trying to edit.

Ie.

root_url/countries/france
root_url/paris/some_place
root_url/paris

Here's my code to be more precise.

resources :countries do
  resources :cities
end

resources :cities do
  resources :places, :reviews
end

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

That seems to work perfectly accept when I try to edit records. The html is as below:

<% form_for @city do |f| %>
<% end %>

Produces:

<form accept-charset="UTF-8" action="/kiev" class="edit_city" id="edit_city_3581" method="post">

Which would only work if it were:

<form accept-charset="UTF-8" action="/cities/kiev" class="edit_city" id="edit_city_3581" method="post">

I know I could simply provide a more advanced form_for route explicitly to get around this, but I'm wondering if there's something better to do in my routes.rb to make my life easier rather than patching?

Thanks

holden
  • 13,471
  • 22
  • 98
  • 160

1 Answers1

1

How about if you renamed your custom routes like this and let normal routes handle edit etc.

get ':id' => 'cities#show', :as => :city_shortcut
get ':city_id/:id' => 'places#show', :as => :city_place_shortcut
Heikki
  • 15,329
  • 2
  • 54
  • 49
  • I thought about that, and that may be the way to go. I'm just wondering if it's better that way or renaming the form_for routes. If I use :city_shortcut I'll have to change all the link_to, etc. vs form_for. I'm looking for pros / cons. – holden Jan 16 '11 at 14:45
  • 1
    I would keep the default behaviour for resources and only modify the bits and pieces that are required for this custom enhancement. Otherwise everything becomes custom and harder to follow. – Heikki Jan 16 '11 at 15:13