Rails 5.1 group_by year and month and show both
我正在使用rails应用程序来支付我的费用。
我有3个模型,如:用户,支出和货币。
我可以通过表单创建支出,我有标题,描述,金额,currency_id,user_id。所有的关系都是有效的。
我使用来自用户控制器的
1 2 3 4 5 6 7 8 9 10 11 12 | Today Expense 1... Expense 2... January ... ... ... December ... ... |
等等...
我想在月份旁边添加年份,以便它显示如下:
1 2 3 4 5 6 7 8 9 10 11 | Today ... ... January 2018 ... ... December 2017 ... ... |
除此之外,我希望这个月和每年都能成为一个链接,这样当您点击它时,您将转到一个新页面,仅显示该特定月份的所有费用。
多数民众赞成。是的,我也使用will_paginate。
现在我做的是这个,让我从
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <% 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> Today <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> <%= Date::MONTHNAMES[hash_elements.first] %> <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 %> |
对不起那个巨大的代码,我希望你不要盲目......
我怎么能设法获得月份和年份,并在此之后建立链接。我已经尝试过一些东西,比如在
然后我就好了,好吧,也许我应该去控制器并在这个变量
好吧,我有点失落,任何帮助或暗示将不胜感激。
非常感谢
好的,我把一切都搞定了!
也许有些人想看看它是怎么样的,所以我们走了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 => @[email protected]_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 |
我更改了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | <% 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 </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"> <%= 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"> <% 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 %> |
我使用
在月/年链接旁边,我添加了一个链接来打印关于这些信息的pdf,我认为它可以很方便。 我在pdf的show视图中使用了我使用的相同变量。 田田! 真棒!
如果你没有得到什么,我希望很清楚,只需写评论!