Rails:form_for和单选按钮

时间:2013-03-26 15:06:09

标签: ruby-on-rails form-for

我的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>

由于某种原因,即使在数据库中选择了用户的multiple_classes属性为true,也始终检查“否”(multiple_classes是数据库中的布尔类型)

2 个答案:

答案 0 :(得分:12)

好的,如果您只想使用radio_buttons,请检查以下内容并告诉我这些是否有效。

<%= 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' %>

10加上引号并尝试....

它不是答案......它只是一个想法......

答案 1 :(得分:0)

始终选中“否”的问题是拼写错误。

如果您还原@user.multiple_classes? "No",如果@user.multiple_classes?true,则取消选中复选框,以便下一个代码块可以正常工作:

<%= 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' %>