Search code examples
ruby-on-railsruby-on-rails-3routesnested-routescustom-routes

pseudo-complex routes in rails 3


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


Solution

  • 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