Rails 5.1 group_by年和月,并显示两者

我正在为我的费用开发一个rails应用程序。
我有3个模型,如:用户,支出和货币。 我可以通过表单创建支出,我有标题,描述,金额,currency_id,user_id。 所有的关系都是有效的。

我使用来自用户控制器的show.html.erb来显示current_user的所有费用。 这很好。 然后我将费用分组在一起并按日期排序。 我实际上做了两组,第一组是今天和其他组。 因此,在显示屏上显示我今天创建的费用列表(第一组),然后是月份列表,其中包含前一个日期(第二组)的费用。 现在它显示了这个:

 Today Expense 1... Expense 2... January ... ... ... December ... ... 

等等…

我想在月份旁边添加年份,以便它显示如下:

 Today ... ... January 2018 ... ... December 2017 ... ... 

除此之外,我希望将这个月和年份作为一个链接,这样当您点击它时,您将转到一个新页面,仅显示该特定月份的所有费用。

多数民众赞成。 是的,我也使用will_paginate。

现在我做的就是这个,让我们从我的UsersController开始:

 class UsersController  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):

  

Today

Title Description Amount Currency Created Actions

Title Description Amount Currency Created Actions

对不起那个巨大的代码,我希望你不要盲目……

我怎么能设法获得月份和年份,并在此之后建立链接。 我已经尝试过一些东西,比如在show修改这个特定的行: 。 我得到的是Date :: MONTHNAMES将实际的月份数字更改为月份名称和hash_elements.first给出了实际的月份数,其中hash_elements.last为我提供了有关我的费用的所有信息。
然后我就好了,好吧,也许我应该去控制器并在这个变量中添加年份@month_wise_sorted_spendings = @user_spendings.group_by{ |t| t.date.month } @month_wise_sorted_spendings = @user_spendings.group_by{ |t| t.date.month } 。 这个group_by现在只对这个月进行分组,所以我尝试添加t.date.year ,但是在视图中我不能使用Date :: MONTHNAMES。 所以这里没有成功……

好吧,我有点失落,任何帮助或暗示将不胜感激。
非常感谢

好的家伙,我把一切都搞定了! 也许有些人想看看它是怎么样的,所以我们走了

  def show @user_spendings = @current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page => 15) #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| (Date::MONTHNAMES[t.date.month] + " " + t.date.year.to_s) } end end def detail if params[:month] @date = Date.parse("#{params[:month]}") # to get the first day of the month @user_spendings_month = @current_user.spendings.order('date DESC').paginate(:page => params[:page], :per_page => 30).where(:date => @date..@date.end_of_month) # get posts for the month @user_amount = Currency.joins(:spendings). select(:symb, 'SUM(spendings.amount) AS amount'). where(spendings: { id: @user_spendings_month.map(&:id) }). group(:symb) else @user_spendings_month = @current_user.spendings.all.order('date DESC') end respond_to do |format| format.html format.pdf { render template: 'views/users/detail', pdf: "eXpenses #{params[:month]}", file: "#{Rails.root}/app/views/users/detail.pdf.erb" } end end 

我更改了@month_wise_sorted_spendings变量以符合我的需要:按名称和年份的月份。 我还添加了def详细信息,因为我希望能够将所有月份/年份的link_to转到其他页面并查看有关本月的具体数据。

  <% if @grouped_spendings.present? && @grouped_spendings[true].present? %> 

Today

<% @grouped_spendings[true].each do |spending| %> <% end %>
Title Description Amount Currency Created Actions
<%= spending.title %> <%= spending.description %> <%= '%.02f' % spending.amount %> <%= spending.currency.symb %> <%= spending.date.strftime('%d %B %Y') %>
<% end %> <% if @month_wise_sorted_spendings.present? %> <% @month_wise_sorted_spendings.each do |month,spending|%> <% date_in_link = " #{month}" %>

<%= link_to user_detail_path(@current_user, :month => month), class: "btn btn-secondary col-1" do %> <%= date_in_link %> <% end %> <%= link_to user_detail_path(@current_user, :month => month, format: "pdf"), class: "btn btn-secondary col-1" do %> PDF <% end %>

<% spending.each do |spending| %> <% end %>
Title Description Amount Currency Created Actions
<%= spending.title %> <%= spending.description %> <%= '%.02f' % spending.amount %> <%= spending.currency.symb %> <%= spending.date.strftime('%d %B %Y') %>
<% end %> <% end %>

我使用month param来获取URL中的日期,并通过@user_spendings_month@page使用到新页面。 在月/年链接旁边,我添加了一个链接来打印关于这些信息的pdf,我认为它可以很方便。 我在pdf的show视图中使用了我使用的相同变量。 田田! 真棒!
如果你没有得到什么,我希望它足够清楚,只需写评论!