Ruby on rails 按日期列出小计的Rails摘要

Ruby on rails 按日期列出小计的Rails摘要,ruby-on-rails,Ruby On Rails,我正在编写我的第一个Rails视图,它按日期总结数据。我希望每个日期有一行,列为该日期汇总 我已经能够让它工作了。但是,这是一个笨拙的编码。这就是我所拥有的: <h3>Carwings Daily Summary</h3> <table class="display dataTable table table-striped table-bordered" id="dataTable2"> <thead> <tr>

我正在编写我的第一个Rails视图,它按日期总结数据。我希望每个日期有一行,列为该日期汇总

我已经能够让它工作了。但是,这是一个笨拙的编码。这就是我所拥有的:

<h3>Carwings Daily Summary</h3>
<table class="display dataTable table table-striped table-bordered"     id="dataTable2">
  <thead>
  <tr>
    <th>Date</th>
    <th># Trips</th>
    <th>E Consumption (kWh)</th>
    <th>E Regeneration (kWh)</th>
    <th>E Total (kWh)</th>
    <th>Distance (Miles)</th>
    <th>Energy Economy (Miles/kWh)</th>
    <th>CO2 Emission Reduction (lbs)</th>
  </tr>
  </thead>
  <tbody>
  <% trips = 0 %>
  <% consumption = 0 %>
  <% regen = 0 %>
  <% total = 0 %>
  <% distance = 0 %>
  <% economy = 0 %>
  <% emissions = 0 %>
  <% sumdate = nil %>
  <% @carwings.each.with_index do |carwing, index| %>
      <% sumdate = carwing.date if index == 0 %>
      <% if carwing.date == sumdate %>
          <% trips = trips + 1 %>
          <% consumption = consumption + carwing.e_consumption %>
          <% regen = regen + carwing.e_regen %>
          <% total = total + carwing.e_total %>
          <% distance = distance + carwing.distance %>
          <% economy = economy + carwing.economy %>
          <% emissions = emissions + carwing.emission_reduction %>
      <% else %>
          <tr>
            <td class="nowrap"><%= sumdate %></td>
            <td><%= trips %></td>
            <td><%= consumption %></td>
            <td><%= regen %></td>
            <td><%= total %></td>
            <td><%= distance %></td>
            <td><%= economy %></td>
            <td><%= emissions %></td>
          </tr>
          <% trips = 1 %>
          <% consumption = carwing.e_consumption %>
          <% regen = carwing.e_regen %>
          <% total = carwing.e_total %>
          <% distance = carwing.distance %>
          <% economy = carwing.economy %>
          <% emissions = carwing.emission_reduction %>
          <% sumdate = carwing.date %>
      <% end %>
  <% end %>
  <tr>
    <td class="nowrap"><%= sumdate %></td>
    <td><%= trips %></td>
    <td><%= consumption %></td>
    <td><%= regen %></td>
    <td><%= total %></td>
    <td><%= distance %></td>
    <td><%= economy %></td>
    <td><%= emissions %></td>
  </tr>
  </tbody>
</table>
Carwings每日摘要
日期
#旅行
电能消耗(kWh)
E再生(kWh)
E总计(kWh)
距离(英里)
能源经济性(英里/千瓦时)
二氧化碳减排(lbs)
一定有更好的办法

建议

谢谢你的帮助

一些次要的东西:

trips = trips + 1
# is better written as:
trips += 1
erb标签可以是多行的,例如:

<% if blah
     do_something
     something else
   end %>
是的-这是次要的东西-但是清理次要的东西,它会给你一个你想要做的更好的形状

也许,如果您以描述性伪代码的形式向我们提供了您的逻辑(因此我们不只是猜测您要做什么),那么我们也可以为您的代码提供更好的结构

就个人而言:我建议在你的控制器(甚至是你的
carwing
model)中设置所有这些数据,以免它们进入视图。我会使用一个散列-其中,
carwnig.date
是键,其余的都是另一个散列,例如:

data = Hash.new({:trips => 0, :consumption => 0}) # google initialising hashes
@carwings.each do |carwing|
  data[carwing.date][:trips] += 1
  data[carwing.date][:consumption] += carwing.e_consumption
  # etc
end 
data = Hash.new({:trips => 0, :consumption => 0}) # google initialising hashes
@carwings.each do |carwing|
  data[carwing.date][:trips] += 1
  data[carwing.date][:consumption] += carwing.e_consumption
  # etc
end