我有一个名为“step”的模型。在我的步骤模型中,我允许用户创建多项选择题。结果我有四个数据库列,从一到四选择。我正在尝试获取这些数据库列并将它们以带有单选按钮的问题的形式放置。我的问题是 @step.choice_one 没有显示(其他人也没有)。此外,单选按钮显示,但它们彼此不相关,因为它允许我单击每个按钮而不会禁用其他按钮。任何帮助将不胜感激。
<%= fields_for :steps do |f| %>
<div class="multipleChoice">
<div>
<%= f.label :choice_one, "1)" %>
<%= f.radio_button :choice_one, @step.choice_one, :checked => true, class: 'icheck' %>
</div>
<div>
<%= f.label :choice_two, "2)" %>
<%= f.radio_button :choice_two, @step.choice_two, class: 'icheck' %>
</div>
<div>
<%= f.label :choice_three, "3)" %>
<%= f.radio_button :choice_three, @step.choice_three, class: 'icheck' %>
</div>
<div>
<%= f.label :choice_four, "4)" %>
<%= f.radio_button :choice_four, @step.choice_four, class: 'icheck' %>
</div>
</div>
<% end %>
最佳答案
这里的问题是您对一组 4 个不同的字段使用单选按钮组,而不是在一个字段上使用 4 个选项。因此,Rails 为每个对象赋予了不同的 name
属性。
尝试为每个 radio_button
赋予相同的名称:'step',例如:
<%= fields_for :steps do |f| %>
<div class="multipleChoice">
<div>
<%= f.label :choice_one, "1) #{@step.choice_one}" %>
<%= f.radio_button :choice_one, @step.choice_one, {name: 'step', checked: true, class: 'icheck'} %>
</div>
<div>
<%= f.label :choice_two, "2) #{@step.choice_two}" %>
<%= f.radio_button :choice_two, @step.choice_two, {name: 'step', class: 'icheck'} %>
</div>
<div>
<%= f.label :choice_three, "3) #{@step.choice_three}" %>
<%= f.radio_button :choice_three, @step.choice_three, {name: 'step', class: 'icheck'} %>
</div>
<div>
<%= f.label :choice_four, "4) #{@step.choice_four}" %>
<%= f.radio_button :choice_four, @step.choice_four, {name: 'step', class: 'icheck'} %>
</div>
</div>
<% end %>
我使用 1
到 4
作为值,但您可以选择您想要的值。
关于html - 单选按钮不显示数据库信息,并且在选择另一个单选按钮时也不会禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18706594/