4

我有以下内容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在数据库中选择了用户的属性为真(在数据库multiple_classes中是布尔类型),也始终检查“否”

4

2 回答 2

13

好的,如果您只想使用 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尝试....

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

于 2013-03-26T15:21:45.570 回答
0

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

如果您恢复@user.multiple_classes?"No",则复选框将被取消选中 if @user.multiple_classes?is 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' %>
于 2016-06-03T14:44:48.650 回答