routing scope problem with form_for (partial)

狂风中的少年 提交于 2019-12-13 13:18:11

问题


Trying to route:

scope :shortcut do
 resources :text_elems
end

Using basic scaffold with form partial

*_form.html.erb*
<%= form_for(@text_elem, :shortcut => @shortcut) do |f| %> 
...

Problem is: When I call the edit action, the form html shows as:

<form ... action="/25/text_elems/25">

Note: The new action renders the form action correctly:

<form ... action="/home/text_elems">

So it appears that my :shortcut param is getting trumped by the :id param when form_for processes it's block. Now I am able to get the action to correctly route with the :shortcut param if I manually make the :url => {...} in the form_for block, but I would prefer to keep the code dry, plus I want to report this problem to rails if it is indeed a bug.

Can anyone else confirm this as a bug?


回答1:


Actually, you can pass the values as a full hash, rather than trying to rely on the default to_param (which is what gets called if all you do is pass the @text_elem)

<%= form_for({:id => @text_elem.to_param, :shortcut => @shortcut}) do |f| %> 

however, if this is actually a nested-resource, you could also do:

<%= form_for([@shortcut, @text_elem]) do |f| %> 



回答2:


I was having the same issues and none of the above answers helped.

The last answer on this page worked for me though...

https://rails.lighthouseapp.com/projects/8994/tickets/6736-problem-with-scoped-routes-and-form_for-helper



来源:https://stackoverflow.com/questions/3371701/routing-scope-problem-with-form-for-partial

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