问题
I have the following routing set up in my app (forms belong to a site):
map.resources :sites do |site|
site.resources :forms
end
However, when I try to go to a path for edit (or such) for a form using the helpers (e.g.
edit_site_form_path(form)
or
<%= link_to 'Show', [:site, form] %>
my URLs are coming out with the ID's swapped over ( /sites/5/forms/1 ) where 5 is the form Id, and 1 is the site id. This is from the page /sites/1.
Help(?)
回答1:
The edit_site_form_path
method has to have two parameters, the site_id and the form_id. So in your example you are only passing in the form_id. The first parameter is what ever model comes first in the method, in this case it is site
. The second parameter is the form_id.
A revamped path method might look like this
edit_site_form_path(form.site, form)
(assuming you have a model Form
which belongs_to :site
)
来源:https://stackoverflow.com/questions/761360/strange-rails-routing-behaviour-two-ids-swapped-around-in-nested-resources