ruby - rails: organizing data into a table using for -
i have following view:
<table class="fixed"> <tr> <th>student name</th> <!-- create many <th> there evaluations --> <% @eval_count.times |i| %> <th>evaluation <%= i+1 %></th> <% end %> <th>student average <br />(for goal)</th> </tr> <% 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 score each consecutive evaluation --> <% @eval_count.times |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) |group|%> <%= (group.sum.to_f/group.size).round(2) %> <% end %>
which renders following:
i want put average each student in last column, @ss_scores
variable , calling on doesn't work. when for
loop has finished, @ss_scores
can worked nicely in bottom of screenshot. idea how better?
try emptying array everytime, using [] , calculate average inline, below
<td><%= @ss_scores.inject(0.0) { |sum, el| sum + el } / @ss_scores.size %></td> <% @ss_scores = [] %>
-
<% 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 score each consecutive evaluation --> <% @eval_count.times |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 %>
Comments
Post a Comment