Rails表单未将单选按钮值传递给params哈希 [英] Rails form not passing radio button values to params hash

查看:83
本文介绍了Rails表单未将单选按钮值传递给params哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无数次在应用程序中使用了单选按钮,并且从未遇到过此问题,因此我假设我没有注意到一个非常愚蠢的错误.表单可以很好地提交所有其他模型的属性,但是由于某种原因,:referral_type不会显示在服务器输出的参数中,并且当然不会保存到数据库中.

I have used radio buttons in my app countless times, and never have experienced this issue, so I am assuming I am oblivious to a very dumb mistake. The form submits all the other model's attributes just fine, but for some reason, :referral_type just does not show up in params in the server output, and it of course is not saving to the db.

= form_for(@writer, :html => { :class => "user_new", :id => "user_new" }) do |f|
  - unless @writer.approved?
    .field.divider
      %label How did you hear about us?
      .writer-channel-input-container
        = f.radio_button :referral_type, "Searching Google"
        = f.label :referral_type, "Searching Google", value: "Searching Google"
      .writer-channel-input-container
        = f.radio_button :referral_type, "CraigsList"
        = f.label :referral_type, "CraigsList", value: "CraigsList"
      .writer-channel-input-container
        = f.radio_button :referral_type, "FlexJobs"
        = f.label :referral_type, "FlexJobs", value: "FlexJobs"
      .writer-channel-input-container.referral-detail
        = f.radio_button :referral_type, "A Forum"
        = f.label :referral_type, "A Forum", value: "A Forum"
      .writer-channel-input-container.referral-detail
        = f.radio_button :referral_type, "A Friend"
        = f.label :referral_type, "A Friend", value: "A Friend"
      .writer-channel-input-container.referral-detail
        = f.radio_button :referral_type, "Other"
        = f.label :referral_type, "Other", value: "Other"
      .referral-detail-input-container{ hidden: "hidden" }
        = f.text_field :referral_detail, disabled: "disabled", placeholder: "Please specify..."

它会生成以下html,看上去与预期的完全一样:

It generates the following html, which appears to be exactly as intended:

<form accept-charset="UTF-8" action="/writers" class="user_new" id="user_new" method="post">
  <div style="margin:0;padding:0;display:inline">
  <input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="fWmxanHllFr0ufsxjEeZSKT6UQyRrLarIB/HOqWLxDA=" /></div>
  <div class='field divider'>
    <label>How did you hear about us?</label>
    <div class='writer-channel-input-container'>
      <input id="writer_referral_type_searching_google" name="writer[referral_type]" type="radio" value="Searching Google" />
      <label for="writer_referral_type_searching_google">Searching Google</label>
    </div>
    <div class='writer-channel-input-container'>
      <input id="writer_referral_type_craigslist" name="writer[referral_type]" type="radio" value="CraigsList" />
      <label for="writer_referral_type_craigslist">CraigsList</label>
    </div>
    <div class='writer-channel-input-container'>
      <input id="writer_referral_type_flexjobs" name="writer[referral_type]" type="radio" value="FlexJobs" />
      <label for="writer_referral_type_flexjobs">FlexJobs</label>
    </div>
    <div class='writer-channel-input-container referral-detail'>
      <input id="writer_referral_type_a_forum" name="writer[referral_type]" type="radio" value="A Forum" />
      <label for="writer_referral_type_a_forum">A Forum</label>
    </div>
    <div class='writer-channel-input-container referral-detail'>
      <input id="writer_referral_type_a_friend" name="writer[referral_type]" type="radio" value="A Friend" />
      <label for="writer_referral_type_a_friend">A Friend</label>
    </div>
    <div class='writer-channel-input-container referral-detail'>
      <input id="writer_referral_type_other" name="writer[referral_type]" type="radio" value="Other" />
      <label for="writer_referral_type_other">Other</label>
    </div>
    <div class='referral-detail-input-container' hidden='hidden'>
      <input disabled="disabled" id="writer_referral_detail" name="writer[referral_detail]" placeholder="Please specify..." size="30" type="text" />
    </div>

我期待你们中的一个指出我的遗漏,谢谢!

I look forward to one of you pointing out my overlook, thanks!

推荐答案

经过4天的摸索,我发现了问题所在.根据: https://code.google.com/p/chromium/问题/详细信息?id = 244645 ,这是Chrome自动填充功能在某些情况下会取消选中单选按钮和复选框的错误.禁用自动填充功能可以解决此问题.它应该在Chrome v28中修复(我目前正在使用版本27.0.1453.116).希望这可以节省一些时间.

After 4 days of messing with this I found the issue. According to: https://code.google.com/p/chromium/issues/detail?id=244645, it is a bug with Chrome autofill removing the check of radio buttons and checkboxes in certain circumstances. Disabling autofill fixed the issue. It should be fixed in Chrome v28 (I am currently using Version 27.0.1453.116). Hope this saves someone some time.

这篇关于Rails表单未将单选按钮值传递给params哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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