Rails:form_for中的隐藏字段未将参数发送到控制器
我正在我的视图助手中制作一个 form_for
,它将让一个用户从组中提升另一个用户。
def promote_button_for(group, user)
membership = group.get_membership( user )
form_for membership, :url => group_membership_path( group, membership ) do |f|
f.hidden_field :group_creator
hidden_field_tag 'test', '1'
f.submit( "Promote", :onclick => "return confirm(\"Are you sure you want to promote #{user.email} to an officer?\")" )
end
end
当我通过按钮提交表单时,我似乎没有将任何隐藏字段参数发送到控制器。
Started POST "/groups/1/memberships/6" for 127.0.0.1 at 2011-02-01 01:45:32 -0600
Processing by MembershipsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VQl/rVX8OVOETv2HE7KtopUc3B19ShoMkUhjJwNlaZs=", "commit"=>"Promote", "group_id"=>"1", "id"=>"6"}
生成的 Html 如下所示:
<form accept-charset="UTF-8" action="/groups/1/memberships/6" class="edit_membership" id="edit_membership_6" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="VQl/rVX8OVOETv2HE7KtopUc3B19ShoMkUhjJwNlaZs=" />
</div>
<input id="membership_submit" name="commit" onclick="return confirm("Are you sure you want to promote [email protected] to an officer?")" type="submit" value="Promote" />
</form>
任何帮助将不胜感激,
谢谢!
I have a form_for
I'm making in my view helper that is going to let one user promote another user from a group.
def promote_button_for(group, user)
membership = group.get_membership( user )
form_for membership, :url => group_membership_path( group, membership ) do |f|
f.hidden_field :group_creator
hidden_field_tag 'test', '1'
f.submit( "Promote", :onclick => "return confirm(\"Are you sure you want to promote #{user.email} to an officer?\")" )
end
end
When I submit the form via the button, I don't seem to get any of the hidden field parameters sending to the controller.
Started POST "/groups/1/memberships/6" for 127.0.0.1 at 2011-02-01 01:45:32 -0600
Processing by MembershipsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"VQl/rVX8OVOETv2HE7KtopUc3B19ShoMkUhjJwNlaZs=", "commit"=>"Promote", "group_id"=>"1", "id"=>"6"}
The generated Html looks like:
<form accept-charset="UTF-8" action="/groups/1/memberships/6" class="edit_membership" id="edit_membership_6" method="post">
<div style="margin:0;padding:0;display:inline">
<input name="utf8" type="hidden" value="✓" />
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="VQl/rVX8OVOETv2HE7KtopUc3B19ShoMkUhjJwNlaZs=" />
</div>
<input id="membership_submit" name="commit" onclick="return confirm("Are you sure you want to promote [email protected] to an officer?")" type="submit" value="Promote" />
</form>
Any help would be greatly appreciated,
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能尝试一下吗
Can you try
您遇到此问题是因为
form_for
对象只能看到由最后一个f.submit
标记生成的字符串,而form_for
之间的其他所有内容code> 并且f.submit
丢失。在这种情况下,
form_for
标记不会直接操作视图,因为它基本上只是从promote_button_for
方法返回的字符串。答案是,您只需将生成的标签链接在一起,如下所示:
注意
<< \
,它将所有生成的字符串连接在一起并将它们返回到form_for
。You are having this problem because the
form_for
object is only seeing the string that is generated by the lastf.submit
tag, while everything else between theform_for
and thef.submit
is lost.In this case, the
form_for
tag does not manipulate the view directly, as it is basically just a string that is returned from thepromote_button_for
method.The answer is that you just need to chain the generated tags together, like this:
Notice the
<< \
, which concatenates all the generated strings together and returns them toform_for
.