2013-08-18 44 views
0

我有一個表單,它遍歷每個學生,並根據使用單選按鈕的目標對它們進行評估。每個目標的得分應爲1-5。此刻,除了單選按鈕,一切都可以正常工作 - 在整個表格中,一次只能選擇一個單選按鈕 - 也就是說,如果目標一被標記爲3,則目標二被標記爲4,目標一變得沒有標記。只有一個單選按鈕可以點擊整個表格

代碼如下:

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> 

回答

1

每個radio_button_tag具有相同的名稱。

您可以爲每個按鈕命名score_xx,其中xx是目標的ID。

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

是完美工作,歡呼聲 - 我不得不改變它'比分_#{goal.id}',因爲subject_id是所有三個目標相同: '<%= radio_button_tag「比分_#{ goal.id}「,得分,goal_id:goal.id,student_id:@ student.id%>' – dax

+0

是的,我的不好,編輯答案。很高興工作! – Intrepidd

0

因爲他們都在同一個相同名稱/ ID,您需要範圍嵌套表格與fields_for幫手。

猜猜你的prtial電話和ff.fields_for goal圍繞你的tr每個循環應該做的伎倆添加一個f.fields_for subject do |ff|

相關問題