【发布时间】:2018-01-21 11:31:42
【问题描述】:
我正在开发一款 Rails 应用程序以支付费用。
我有 3 个模型,例如:用户、支出和货币。
我可以通过表单创建支出,我有标题、描述、金额、货币 ID、用户 ID。所有的关系都已建立并正常工作。
我使用来自用户控制器的show.html.erb 来显示来自 current_user 的所有费用。这很好用。然后我将费用分组在一起并按日期排序。我实际上做了两组,第一组是今天,其余的是。因此,在显示屏上,它向我显示了我今天创建的费用列表(第一组),然后是包含上一个日期的费用的月份列表(第二组)。现在它显示了这个:
Today
Expense 1...
Expense 2...
January
...
...
...
December
...
...
等等……
我想在月份旁边添加年份,这样它就会像这样显示:
Today
...
...
January 2018
...
...
December 2017
...
...
此外,我希望这个月和年成为一个链接,这样当您点击它时,您会转到一个新页面,该页面仅显示该特定月份的所有费用。
就是这样。啊,是的,我也用 will_paginate。
现在我做的是这个,让我们从我的UsersController 开始:
class UsersController < ApplicationController
def show
@user_spendings = @current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page => 10)
#Retrives all messages and divides into two groups todays messages and other messages
@grouped_spendings = @user_spendings.group_by{ |t| t.date.to_date == DateTime.now.to_date }
if @user_spendings.present?
#Create month wise groups of messages
@month_wise_sorted_spendings = @user_spendings.group_by{ |t| t.date.month }
end
end
而且我的show 视图有点长(我没有粘贴 will_paginate):
<% if @grouped_spendings.present? && @grouped_spendings[true].present? %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<h3>Today</h3>
<tbody>
<% @grouped_spendings[true].each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% if @month_wise_sorted_spendings.present? %>
<% @month_wise_sorted_spendings.each do |hash_elements|%>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Amount</th>
<th>Currency</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<h3><%= Date::MONTHNAMES[hash_elements.first] %></h3>
<tbody>
<% hash_elements.last.each do |spending| %>
<tr>
<td><%= spending.title %></td>
<td><%= spending.description %></td>
<td><%= '%.02f' % spending.amount %></td>
<td><%= spending.currency.symb %></td>
<td><%= spending.date.strftime('%d %B %Y') %></td>
</tr>
</tbody>
<% end %>
</table>
<% end %>
<% end %>
对不起那个巨大的代码,我希望你不是盲目的......
我怎么能设法得到月份和年份并在此之后将其作为链接。我已经尝试了一些方法,例如修改 show 中的这一特定行:<%= Date::MONTHNAMES[hash_elements.first] %>。我得到的是 Date::MONTHNAMES 将实际月份编号更改为月份名称,而 hash_elements.first 为我提供了实际月份编号,其中 hash_elements.last 为我提供了有关我的费用的所有信息。
然后我想,好吧,也许我应该去控制器并在这个变量@month_wise_sorted_spendings = @user_spendings.group_by{ |t| t.date.month } 中添加年份。这个 group_by 现在只对月份进行分组,所以我尝试添加 t.date.year,但是在视图中我不能使用 Date::MONTHNAMES。所以这里没有成功...
好吧,我有点迷茫,任何帮助或提示将不胜感激。
非常感谢
【问题讨论】:
-
我尝试将 .to_date 放在 UsersController 中,如下所示:
@month_wise_sorted_spendings = @user_spendings.group_by{ |t| t.date.to_date }并在节目中删除了Date::MONTHNAMES。结果我有一个完整的日期,但它现在通过这些日期对费用进行排序,而不是我真正想要的方式。取得进展,但还没有。 -
快到了!我再次更改了它:
@month_wise_sorted_spendings = @user_spendings.group_by{ |t| [Date::MONTHNAMES[t.date.month] + " " + t.date.year.to_s] },现在我得到了这个:["January 2018"]。如何删除那些可怕的 [ ] 和 " " ? :) -
好吧,简单...
@month_wise_sorted_spendings = @user_spendings.group_by{ |t| (Date::MONTHNAMES[t.date.month] + " " + t.date.year.to_s) }使用 ( ) 它就像一个魅力!链接是我唯一剩下的问题。如何将这些链接到仅显示特定月份费用的新页面?
标签: ruby-on-rails date group-by ruby-on-rails-5