我从接受的答案中略微改变了一点(这使我走上正轨,所以我没有提交新的答案):
<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行 - 表头和表数据中的一个。我怎样才能做到这一点?
答案 0 :(得分: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>
尽管如此,您应该将控制器中的加载代码(第一行)移动到模型或其他域对象中。但这是另一个话题,所以我把它留在了问题中。