Rails:form_for中的隐藏字段未向控制器发送参数 [英] Rails: Hidden field in form_for is not sending parameters to controller

查看:72
本文介绍了Rails:form_for中的隐藏字段未向控制器发送参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自己的视图助手中有一个form_for,它将允许一个用户从组中提升另一个用户.

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"}

生成的HTML看起来像:

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="&#x2713;" />
      <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(&quot;Are you sure you want to promote kquiring@gmail.com to an officer?&quot;)" type="submit" value="Promote" />
</form>

任何帮助将不胜感激,

谢谢!

推荐答案

您遇到了这个问题,因为form_for对象仅看到由最后一个f.submit标记生成的字符串,而其他所有form_forf.submit丢失.

You are having this problem because the form_for object is only seeing the string that is generated by the last f.submit tag, while everything else between the form_for and the f.submit is lost.

在这种情况下,form_for标记不会直接操纵视图,因为它基本上只是从promote_button_for方法返回的字符串.

In this case, the form_for tag does not manipulate the view directly, as it is basically just a string that is returned from the promote_button_for method.

答案是,您只需要将生成的标签链接在一起,就像这样:

The answer is that you just need to chain the generated tags together, like this:

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

注意<< \,它将所有生成的字符串连接在一起,并将它们返回到form_for.

Notice the << \, which concatenates all the generated strings together and returns them to form_for.

这篇关于Rails:form_for中的隐藏字段未向控制器发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆