4

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)

7
  • why do you creating two radio buttons? you can do it in a single ain't u? Commented Mar 26, 2013 at 15:09
  • @Vinay - can you give me an example of how to do that? Commented Mar 26, 2013 at 15:11
  • do you need two radio buttons in the form or one? Commented Mar 26, 2013 at 15:12
  • @Vinay - well, the user can select whether they teach multiple classes or not by selecting a radio button Commented Mar 26, 2013 at 15:13
  • 2
    I believe @Vinay is suggesting you could just use a single checkbox. Commented Mar 26, 2013 at 15:17

2 Answers 2

14

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

Sign up to request clarification or add additional context in comments.

Comments

0

The problem with always checked "No" is in a typo.

If you revert @user.multiple_classes? for "No", checkbox will be unchecked if @user.multiple_classes? is true, so the next code chunk should work fine:

<%= 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: [email protected]_classes?, class: 'multiple_classes', data: { question: 'How many kids do you teach?' } %> <%= f.label :multiple_classes, "No", class: 'multiple_classes' %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.