html - rails : how to divide data into separate td's after x records

标签 html ruby-on-rails ruby-on-rails-3

更新的解决方案:

我对接受的答案做了一些改动(这让我走上了正确的 rails ,所以我没有提交新的答案):

<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>]}

结束编辑

此刻,它输出这个:

enter image description here

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

最佳答案

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

<% 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>

不过,您应该将加载代码(第一行)跨 Controller 移动到模型或其他域对象中。但这是另一个话题,所以我将其保留在问题中。

关于html - rails : how to divide data into separate td's after x records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429663/

相关文章:

javascript - 如何在 HTML5 中从一个窗口控制另一个窗口?

html - Powershell正则表达式匹配除第一个以外的字符串

html - anchor 标记继承自非预期元素

ruby-on-rails - 强制所有操作的渲染格式

ruby-on-rails - 列 "pg_search_***"必须出现在 GROUP BY 子句中或用于聚合函数中

ruby-on-rails - 为什么我的 Rails 应用程序中会出现使用 force_ssl 的无限重定向循环?

css - 将 Bootstrap 表单的提交按钮修改为自定义 css

javascript - anchor 跳跃而不是平滑滚动

ruby-on-rails - 测试字段在 mongoid 中键入一个模型

ruby-on-rails - mongoid - 覆盖现有字段警告