Rails 5.1 group_by年和月并显示两者

时间:2018-01-21 11:31:43

标签: ruby-on-rails date group-by ruby-on-rails-5

我正在使用rails app来支付我的费用 我有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 < 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。所以这里没有成功......

嗯,我有点失落,任何帮助或暗示都会受到赞赏 非常感谢

1 个答案:

答案 0 :(得分:0)

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

public override int SaveChanges()

我更改了Bearer变量以符合我的需要:按名称和年份的月份。我还添加了def详细信息,因为我希望能够{/ 1}}所有月份/年度转到另一页并查看有关本月的具体数据。

    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来获取网址中的日期,并通过link_to <% if @grouped_spendings.present? && @grouped_spendings[true].present? %> <table class= "table table-striped table-bordered table-responsive"> <thead> <tr> <th>Title</th> <th>Description</th> <th>Amount</th> <th>Currency</th> <th>Created</th> <th>Actions</th> </tr> </thead> <h3 class="btn btn-secondary col-1">Today</h3> </br></br> <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 |month,spending|%> <table class= "table table-striped table-bordered table-responsive"> <thead> <tr> <th>Title</th> <th>Description</th> <th>Amount</th> <th>Currency</th> <th>Created</th> <th>Actions</th> </tr> </thead> </br> <% date_in_link = " #{month}" %> <p><%= link_to user_detail_path(@current_user, :month => month), class: "btn btn-secondary col-1" do %> <i class="fa fa-link"></i> <%= date_in_link %> <% end %> <%= link_to user_detail_path(@current_user, :month => month, format: "pdf"), class: "btn btn-secondary col-1" do %> <i class="fa fa-file-pdf-o"></i> PDF <% end %></p> </br> <tbody> <% spending.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 %> </div> 使用新页面。 在月/年链接旁边,我添加了一个链接来打印关于这些信息的pdf,我认为它可以很方便。我在pdf的show视图中使用了我使用的相同变量。田田!真棒! 如果你没有得到什么,我希望很清楚,只需写一条评论!