开发者

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜