Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: form_for and radio buttons

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)

like image 696
dennismonsewicz Avatar asked Dec 12 '22 16:12

dennismonsewicz


1 Answers

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

like image 144
Sri Avatar answered Dec 31 '22 11:12

Sri