-1

我有以下看法:

<table class="fixed">                                                                            
  <tr>                                                                             
    <th>Student Name</th>
    <!-- create as many <th> as there are evaluations -->
    <% @eval_count.times do |i|  %>                                                 
      <th>Evaluation <%= i+1 %></th>
    <% end %>    
    <th>Student Average <br />(for this goal)</th>

  </tr>                                                                           

  <% for eval in @evals %>                     
    <tr class="<%= cycle("odd", "even", name: "evals")%>">
        <!-- eval returns { s_id [eval],[eval]} -->
      <td><%= eval[1].first.student.name%></td> 
      <!-- in each student's row, print the score for each consecutive evaluation -->
      <% @eval_count.times do |i|  %>                                                 
        <td><%= eval[1][i].score %><% @ss_scores << eval[1][i].score %></td> 
      <% end %>
      <td><%= @ss_scores %></td>
    </tr>             
   <% reset_cycle("evals") %>   
   <% end %>                                                                        
</table>

<% @ss_scores.in_groups(@student_count, false) do |group|%>
    <%= (group.sum.to_f/group.size).round(2) %>                                                    
<% end %>

呈现以下内容:

在此处输入图像描述

我想把每个学生的平均值放在最后一列,但@ss_scores它是一个变量,所以在它上面调用任何东西都不起作用。但是当for循环完成时,@ss_scores可以像屏幕截图底部一样很好地使用。知道如何更好地做到这一点吗?

4

1 回答 1

1

尝试每次清空数组,使用 [] 并计算平均内联,如下所示

 <td><%= @ss_scores.inject(0.0) { |sum, el| sum + el } / @ss_scores.size %></td>
 <% @ss_scores = [] %>

-

<% for eval in @evals %>                      
        <tr class="<%= cycle("odd", "even", name: "evals")%>">
            <!-- eval returns { s_id [eval],[eval]} -->
          <td><%= eval[1].first.student.name%></td> 
          <!-- in each student's row, print the score for each consecutive evaluation -->
          <% @eval_count.times do |i|  %>                                                 
            <td><%= eval[1][i].score %>
              <% @ss_scores << eval[1][i].score %>
            </td> 
          <% end %>
          <td><%= @ss_scores.inject(0.0) { |sum, el| sum + el } / @ss_scores.size %></td>
          <% @ss_scores = [] %>
        </tr>             
       <% reset_cycle("evals") %>  

    <% end %>
于 2013-08-26T11:35:34.340 回答