简体   繁体   English

奇怪的Rails路由行为:在嵌套资源中交换两个ID

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

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 (eg 但是,当我尝试使用帮助程序(例如,使用帮助程序)转到编辑(或类似)表单的路径时

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. 我的网址是ID交换过的(/ sites / 5 / forms / 1),其中5是表单ID,1是网站ID。 This is from the page /sites/1. 这是来自page / sites / 1。

Help(?) 救命(?)

The edit_site_form_path method has to have two parameters, the site_id and the form_id. edit_site_form_path方法必须有两个参数:site_id和form_id。 So in your example you are only passing in the form_id. 因此,在您的示例中,您只传递form_id。 The first parameter is what ever model comes first in the method, in this case it is site . 第一个参数是模型在方法中首先出现的,在这种情况下它是site The second parameter is the form_id. 第二个参数是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 ) (假设你有一个belongs_to :site的模型Form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM