【问题标题】:Rails 5.1 group_by year and month and show bothRails 5.1 group_by 年和月并同时显示
【发布时间】: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 中的这一特定行:&lt;%= Date::MONTHNAMES[hash_elements.first] %&gt;。我得到的是 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


【解决方案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 变量以满足我的需要:按名称和年份显示月份。我还添加了 def 详细信息,因为我希望能够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>

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

【讨论】:

  • 谢谢!我赢了! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
相关资源
最近更新 更多