【问题标题】:Only one radio button can be clicked for entire form整个表单只能单击一个单选按钮
【发布时间】:2013-08-20 05:59:49
【问题描述】:

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

代码如下:

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>

【问题讨论】:

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


    【解决方案1】:

    因为它们都具有相同的名称/ID,所以您需要使用 fields_for 助手来限定嵌套表单。

    猜猜在您的 prtial 呼叫周围添加一个 f.fields_for subject do |ff| 并在您的 tr 周围添加一个 ff.fields_for goal 每个循环应该可以解决问题。

    【讨论】:

      【解决方案2】:

      每个radio_button_tag 都具有相同的名称。

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

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

      【讨论】:

      • 效果很好,干杯 - 我不得不将其更改为 score_#{goal.id},因为所有三个目标的 subject_id 都是相同的:&lt;%= radio_button_tag "score_#{goal.id}", score, goal_id: goal.id, student_id: @student.id %&gt;
      猜你喜欢
      • 1970-01-01
      • 2017-06-28
      • 2012-12-28
      • 2018-06-27
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2012-04-23
      相关资源
      最近更新 更多