Rails form - radio button checked status not applying correctly
<p>I am having an issue with several radio button groups in my Rails app where the <code>checked</code> status is not being correctly applied.  Let me break it down for you...</p>

<p>The form has four groups of radio buttons in a row, each generated with the <code>collection_radio_buttons</code> form helper:</p>

<pre><code>Left buffer width
&lt;%= collection_radio_buttons("", "", @bufferWidthScore, :id, :display) do |b| %&gt;
    &lt;%=j b.label(class: "left_buffer_width_label") %&gt;
    &lt;%=j b.radio_button(class: "left_buffer_width") %&gt;
&lt;% end %&gt;

Right buffer width&lt;%= collection_radio_buttons("", "", @bufferWidthScore, :id, :display) do |b| %&gt;
    &lt;%=j b.label(class: "right_buffer_width_label") %&gt;
    &lt;%=j b.radio_button(class: "right_buffer_width") %&gt;
&lt;% end %&gt;

Left buffer condition&lt;%= collection_radio_buttons("", "", @bufferConditionScore, :id, :display) do |b| %&gt;
    &lt;%=j b.label(class: "left_buffer_condition_label") %&gt;
    &lt;%=j b.radio_button(class: "left_buffer_condition") %&gt;
&lt;% end %&gt;

Right buffer condition&lt;%= collection_radio_buttons("", "", @bufferConditionScore, :id, :display) do |b| %&gt;
    &lt;%=j b.label(class: "right_buffer_condition_label") %&gt;
    &lt;%=j b.radio_button(class: "right_buffer_condition") %&gt;
&lt;% end %&gt;
</code></pre>

<p>I want to set it so that the first radio button in each group is checked, and I had some success in getting this to work, setting the options hash like so:</p>

<pre><code>Left buffer width&lt;%= collection_radio_buttons("", "", @bufferWidthScore, :id, :display, {checked: @bufferWidthScore.first.id}, {}) do |b| %&gt;
    &lt;%=j b.label(class: "left_buffer_width_label") %&gt;
    &lt;%=j b.radio_button(class: "left_buffer_width") %&gt;
&lt;% end %&gt;
</code></pre>

<p>So this worked for the first group of radio buttons, but when I added it to the second group, only the first radio button from the second group was checked, the first group had no checked buttons.  Ok, I thought that was odd, but as a test, I added this code to the third set of radio buttons, which you'll notice relates to a different model (bufferCondition, rather than bufferWidth) - same problem!  It didn't matter!  Now the first button in the third group was checked, but none of the other buttons in the other groups.</p>

<p>So in summary, if I put this code into all four button groups, only the first button of the last group will be checked, regardless of the model.</p>

<p>Can anyone explain why this is happening and how to solve this?  None of the other behaviors of these button groups seem to cross over each other, why does this one?</p>