2012-01-18 66 views
0

我打印出一个包含行总计的表,但我也希望获得列的总计。下面的代码不起作用,而不是总打印数量,它只是打印出最后一次迭代的值。Rails 3.从数组中获取总计

<% @shipments.each do |shipment| %> 
    <tr> 
    <td style="text-align:center;"><%= shipment.file_number %></td> 
    <td><%= shipment.shipper.company_name %></td> 
    <td><%= shipment.hbl %></td> 
    <td><%= shipment.status %></td> 
    <td><%= shipment.age %></td> 
    <td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td> 
    <td><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td> 
    <td><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td> 
    <td><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td> 
    </tr> 
    <% 
    grand_customer_total = 0 
    grand_customer_amount_paid = 0 
    grand_customer_open_balance = 0 
    grand_customer_total += shipment.invoice.customer_total 
    grand_customer_amount_paid += shipment.invoice.customer_amount_paid 
    grand_customer_open_balance += shipment.invoice.customer_open_balance 
    %> 
    <% if @shipments.last == shipment %> 
    <tr> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <th>Totals</th> 
    <td><%= number_to_currency grand_customer_total %></td> 
    <td><%= number_to_currency grand_customer_amount_paid %></td> 
    <td><%= number_to_currency grand_customer_open_balance %></td> 
    </tr> 
    <% end %> 

回答

2

你的代码不工作的原因是你的变量是在块中定义的,所以它们被认为是块本地变量。一旦块被退出,这些变量被标记为被清除;每次迭代都会重新定义这些变量。它也无助于你在每次迭代时将它们重新赋值为0,但这甚至不会在这里生效,因为每次都没有定义变量。

您可以简单地在块之前定义变量,但这仍然很麻烦。由于Ruby的习惯用法和惯例强调干净且组织良好的代码,所以我偏离了这一点,而是单独计算这些数字,可能在您的控制器中。

@totals = { 
    :overall => @shipments.reduce(0) { |total, shipment| total + shipment.invoice.customer_total }, 
    :paid => @shipments.reduce(0) { |total, shipment| total + shipment.invoice.customer_amount_paid }, 
    :balance => @shipments.reduce(0) { |total, shipment| total + shipment.invoice.customer_open_balance } 
} 

然后,而不是使用@shipments.last比较,你可以做你的出货量表输出后执行以下操作:

<tr> 
    <td colspan="5"></td> 
    <th>Totals</th> 
    <td><%= number_to_currency @totals[:overall] %></td> 
    <td><%= number_to_currency @totals[:paid] %></td> 
    <td><%= number_to_currency @totals[:balance] %></td> 
</tr> 
2

您每次通过循环设置总计为零。在循环之前移动初始化。

1

可以使用红宝石inject

@shipments.inject(0) { |sum, shipment| sum + shipment.invoice.customer_total } 

或保持你的代码的布局,只是初始化@shipments.each环外的grand_customer_*对象,因为您是通过

每次重置他们
1
<% 
grand_customer_total = 0 
grand_customer_amount_paid = 0 
grand_customer_open_balance = 0 
%> 
<% @shipments.each do |shipment| %> 
<tr> 
    <td style="text-align:center;"><%= shipment.file_number %></td> 
    <td><%= shipment.shipper.company_name %></td> 
    <td><%= shipment.hbl %></td> 
    <td><%= shipment.status %></td> 
    <td><%= shipment.age %></td> 
    <td><%= shipment.invoice.read_issued_at unless shipment.invoice.nil? %></td> 
    <td><%= number_to_currency shipment.invoice.customer_total unless shipment.invoice.nil? %></td> 
    <td><%= number_to_currency shipment.invoice.customer_amount_paid unless shipment.invoice.nil? %></td> 
    <td><%= number_to_currency shipment.invoice.customer_open_balance unless shipment.invoice.nil? %></td> 
</tr> 
<% 
grand_customer_total += shipment.invoice.customer_total 
grand_customer_amount_paid += shipment.invoice.customer_amount_paid 
grand_customer_open_balance += shipment.invoice.customer_open_balance 
%> 
<% if @shipments.last == shipment %> 
<tr> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <th>Totals</th> 
    <td><%= number_to_currency grand_customer_total %></td> 
    <td><%= number_to_currency grand_customer_amount_paid %></td> 
    <td><%= number_to_currency grand_customer_open_balance %></td> 
</tr> 
<% end %>