Rails: form_for and radio buttons

dennismonsewicz picture dennismonsewicz · Mar 26, 2013 · Viewed 21.5k times · Source

I have the following in my form_for

<div class="multiple_students">
        <%= f.label :multiple_classes, "Do you teach multiple classes?" %>
        <%= f.radio_button :multiple_classes, 1, checked: @user.multiple_classes?, class: 'multiple_classes', data: { question: 'What is your average class size?' } %> <%= f.label :multiple_classes, "Yes", class: 'multiple_classes' %>
        <%= f.radio_button :multiple_classes, 0, checked: @user.multiple_classes?, class: 'multiple_classes', data: { question: 'How many kids do you teach?' } %> <%= f.label :multiple_classes, "No", class: 'multiple_classes' %>
    </div>
    <div class="number_of_students hide">
        <%= f.label :students %>
        <%= f.text_field :students, :class=>"student_count required digits" %>
    </div>

For some reason the "No" is ALWAYS checked even if the multiple_classes attribute on the user is selected a true in the database (multiple_classes is a boolean type in the database)

Answer

Sri picture Sri · Mar 26, 2013

Ok if you want to use only the radio_buttons, check the following and let me know if thats worked.

<%= f.radio_button :multiple_classes, "1", checked: @user.multiple_classes?, class: 'multiple_classes', data: { question: 'What is your average class size?' } %> <%= f.label :multiple_classes, "Yes", class: 'multiple_classes' %>
<%= f.radio_button :multiple_classes, "0", checked: @user.multiple_classes?, class: 'multiple_classes', data: { question: 'How many kids do you teach?' } %> <%= f.label :multiple_classes, "No", class: 'multiple_classes' %>

put the 1 and 0 with quotes and try....

Its not an answer... its only an idea...