Rails form not passing radio button values to params hash

社会主义新天地 提交于 2019-12-11 04:56:37

问题


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

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!


回答1:


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.



来源:https://stackoverflow.com/questions/17374173/rails-form-not-passing-radio-button-values-to-params-hash

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