Strange Rails Routing behaviour: two ids swapped around in nested resources

吃可爱长大的小学妹 提交于 2019-12-10 22:17:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!