【发布时间】:2021-02-11 06:58:01
【问题描述】:
为了按降序显示记录,我使用了“created_at DESC”,它适用于表的所有条目,即日期列、详细信息列、借方和贷方列,余额除外,它是仍然从上到下计算和显示。但我想从下到上计算和显示。这可以在下图中看到。
expenses_controller.rb
类费用控制器 为了更好地理解,请找到下图的银行对帐单,因为我需要做到这一点。 index.html.erb 欢迎提出任何建议。 提前谢谢你。 def index
@expenses = Expense.order("created_at DESC")
end
<% balance = 0 %>
<div class="container">
<table style="width:100%">
<thead>
<tr>
<th>Date</th>
<th>Particulars</th>
<th>Debit</th>
<th>Credit</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<% @expenses.each do |expense| %>
<tr>
<td><%= expense.date.strftime('%d/%m/%Y') %></td>
<td><%= expense.particulars %></td>
<td class="pos"><%= expense.debit %></td>
<td class="neg"><%= expense.credit %></td>
<% balance += expense.debit.to_f-expense.credit.to_f %>
<% color = balance >= 0 ? "pos" : "neg" %>
<td class="<%= color %>"><%= number_with_precision(balance.abs, :delimiter => ",", :precision => 0) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
【问题讨论】:
-
Expense.order("created_at DESC")表示最新的在顶部,而我想它应该是ASC。无论如何,在这种情况下,我无法弄清楚余额2500, 1500, 2000是从哪里来的。你能忘记代码并展示预期结果背后的逻辑吗? -
感谢您的回复。我希望将最新的放在顶部。余额 2500、1500、2000 是表中显示的预期输出。
-
我已经更新了帖子,以便通过添加真实的银行对帐单来更好地理解。希望对您有所帮助。