整个表单只能单击一个单选按钮

时间:2013-08-18 07:47:18

标签: ruby-on-rails ruby-on-rails-3 forms radio-button

我有一个表格,它遍历每个学生,并使用单选按钮根据目标对其进行评估。每个目标的得分应为1-5。目前,一切正常,除了单选按钮 - 整个表单中一次只能选择一个单选按钮 - 即,如果目标1标记为3,然后目标2标记为4,目标1没有标记。

代码如下:

evaluation

evaluations - new.html.erb

...
<div class="holder2 round clear">
    <%= form_for([@student, @evaluation]) do |f|  %>
      <% @subjects.each do |group, subjects| %>
        <% subjects.each do |subject| %>
          <h3><%= subject.name %></h3>  
          <%= render "goal_eval", :subject => subject, :f => f %>
        <% end %>
      <% end %>
      <%= f.submit "Next student", :class => 'big_button round unselectable' %>
    <% end %>
</div>

goal_eval partial

<table class="fixed">
  <tbody>
      <% subject.goals.each do |goal| %>
      <tr class="<%= cycle("odd", "even", name: "goals")%>">
        <td class="goal_row"><%= goal.goal %></td> 
        <td>
          <% [1, 2, 3, 4, 5].each do |score| %>
            <%= radio_button_tag :score, score, goal_id: goal.id, student_id: @student.id %>
            <%= score %>
          <% end %>
        </td>
      </tr>  
    <% end %>
      <% reset_cycle("goals") %>
  </tbody>
</table>

2 个答案:

答案 0 :(得分:1)

每个radio_button_tag都有相同的名称。

您可以为每个按钮score_xx命名,其中xx是目标的ID。

<%= radio_button_tag "score_#{goal.id}", score, goal_id: goal.id, student_id: @student.id %>

答案 1 :(得分:0)

因为它们在同一个中具有相同的名称/ ID,所以您需要使用fields_for帮助程序来设置嵌套表单的范围。

猜猜在你的prtial调用中添加f.fields_for subject do |ff|并在 tr 周围添加ff.fields_for goal每个循环应该可以解决问题。

相关问题