2

更新的解决方案:

我从接受的答案中稍微改变了一些(这使我走上了正确的轨道,所以我没有提交新的答案):

<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 %></td> 
      <% end %>
    </tr>             
   <% reset_cycle("evals") %>   
   <% end %>                                                                        
</table>

结束更新

目前我认为以下内容(我知道这很丑陋,我只是这样做,所以在我测试时一切都在同一页面上,当它工作时我会清理它!):

<div class="holder2 round">
  <% if @goal.avg.nan? %>
    <p>
      This goal has not been evaluated yet!
    </p>
  <% else %>    
    <%= render 'evaluations_by_goal' %>         
  <% end %> 
</div>

_evaluations_by_goal.html.erb

<% ordered_evals_by_goal = @goal.evaluations.order("eval_number").all.group_by { |g| g.eval_number } %>

<h3>
The overall average for this goal is <%= @goal.avg %>
</h3>
<table>
  <tr>
    <th>Student Name</th>
    <th>Evaluation 1</th>
    <th>Evaluation 2</th>
  </tr>
  <% @scores = [] %>
  <% ordered_evals_by_goal.each do |number, evals| %>
    <% evals.in_groups(evals.count, false) do |group| %>
      <% group.each do |eval| %>
        <tr>
          <td><%= eval.student.name %></td>
          <td><%= eval.score %></td>
          <td>**others here**</td>
        </tr>
      <% end %>
    <% end %>   
  <% end %> 
</table>

编辑:

所以你可以看到哈希的结构,ordered_evals_by_goal输出这个:

{22=>
  [#<Evaluation id: 1702, score: 4, created_at: "2013-08-25 11:00:58", updated_at: "2013-08-25 11:00:58", student_id: 22, goal_id: 28, eval_number: 22>, 
  #<Evaluation id: 1710, score: 3, created_at: "2013-08-25 11:01:08", updated_at: "2013-08-25 11:01:08", student_id: 23, goal_id: 28, eval_number: 22>, 
  #<Evaluation id: 1718, score: 3, created_at: "2013-08-25 11:01:15", updated_at: "2013-08-25 11:01:15", student_id: 24, goal_id: 28, eval_number: 22>, 
  #<Evaluation id: 1726, score: 3, created_at: "2013-08-25 11:01:21", updated_at: "2013-08-25 11:01:21", student_id: 25, goal_id: 28, eval_number: 22>], 
23=>
  [#<Evaluation id: 1734, score: 3, created_at: "2013-08-25 11:02:18", updated_at: "2013-08-25 11:02:18", student_id: 22, goal_id: 28, eval_number: 23>, 
  #<Evaluation id: 1742, score: 3, created_at: "2013-08-25 11:02:27", updated_at: "2013-08-25 11:02:27", student_id: 23, goal_id: 28, eval_number: 23>, 
  #<Evaluation id: 1750, score: 3, created_at: "2013-08-25 11:02:35", updated_at: "2013-08-25 11:02:35", student_id: 24, goal_id: 28, eval_number: 23>, 
  #<Evaluation id: 1758, score: 3, created_at: "2013-08-25 11:02:42", updated_at: "2013-08-25 11:02:42", student_id: 25, goal_id: 28, eval_number: 23>]}

结束编辑

目前,它输出以下内容:

在此处输入图像描述

但是在学生姓名开始循环的地方,我希望分数数据进入空的 td 以便整个表(在这种情况下)总共有 5 行 - 一个表头和四个表数据。我怎样才能做到这一点?

4

1 回答 1

1

要获得想要的输出,您可以将视图代码重构为:

<% evals_by_student = @goal.evaluations.order("eval_number").group_by(&:student_id) %>

<h3>The overall average for this goal is <%= @goal.avg %></h3>

<table>                                                                            
  <tr>                                                                             
    <th>Student Name</th>                                                          
    <th>Evaluation 1</th>                                                          
    <th>Evaluation 2</th>                                                          
  </tr>                                                                           

  <% for student_id, (eval_1, eval_2) in evals_by_student %>                       
    <tr>                                                                           
      <td><%= eval_1.name %></td>                                                  
      <td><%= eval_1.score %></td>                                                 
      <td><%= eval_2.score if eval_2.present? %></td>                              
    </tr>                                                                          
  <% end %>                                                                        
</table>

虽然,您应该将加载代码(第一行)跨控制器移动到模型或其他域对象中。但那是另一个话题,所以我把它留在了问题中。

于 2013-08-25T15:55:08.027 回答