質問の背景
Railsで家計簿アプリを作成中。
Deviseをインストール済み。
Paymentsコントローラを作成済み。
質問
支払い一覧ページを作成しました。
一覧ページには今月の支払いだけを表示するようにしています。
前月・前々月に遡ってデータを表示する機能を搭載したいのですが、どのようにするものでしょうか。
アイディアがなく、お知恵をお借りできましたら幸いです。
該当のソースコード
routes.rb
ruby
1Rails.application.routes.draw do 2 3 resources :payments, only: [:index, :new, :create, :edit, :update, :destroy] 4 5 devise_for :users, :controllers => { 6 :registrations => 'users/registrations', 7 :sessions => 'users/sessions' 8} 9 10devise_scope :user do 11 root :to => "devise/sessions#new" 12 get "user/:id", :to => "users/registrations#detail" 13 get "signup", :to => "users/registrations#new" 14 get "login", :to => "users/sessions#new" 15 get "logout", :to => "users/sessions#destroy" 16 get "forgot", :to => "users/passwords#new" 17end 18 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 19end
payments_controller.rb
class PaymentsController < ApplicationController include PaymentsHelper PER = 5 def index payments_this_month @payments = @payments_this_month.page(params[:page]).per(PER) end def new @payment = current_user.payments.new end def create @payment = current_user.payments.new(payment_params) if @payment.save redirect_to new_payment_path, notice: "支払い「#{@payment.item}」を登録しました。" else render :new end end def edit @payment = current_user.payments.find(params[:id]) end def update @payment = current_user.payments.find(params[:id]) if @payment.update(payment_params) redirect_to payments_path, notice: "支払い「#{@payment.item}」を更新しました。" else render :edit end end def destroy @payment = current_user.payments.find(params[:id]) @payment.destroy redirect_to payments_path, notice: "支払い「#{@payment.item}」を削除しました。" end private def payment_params params.require(:payment).permit(:item, :price, :category) end end
payment.rb
class Payment < ApplicationRecord validates :item, presence: true, length: { maximum: 10} validates :price, presence: true, numericality: { only_integer: true }, length: { maximum: 15} validates :category, presence: true, length: { maximum: 10} belongs_to :user end
index.html.erb
<div class="container pt-3 text-center"> <h2>家計簿一覧</h2> </div> <div class="row py-3 h-75 justify-content-center"> <div class="card" style="width:calc(20% - .25rem)"> <div class="card-body"> <h3><%= this_month.month %>月</h3> </div> </div> </div> <div class="row py-3 justify-content-center"> <div class="card" style="width:calc(50% - .25rem)"> <div class="card-body"> <h3>支払総額 <%= sum_payment_this_month %>円</h3> </div> </div> </div> <div class="container"> <div class="row justify-content-center"> <a class="btn btn-warning w-25 mx-3" href="payments/new" role="button">支払い登録</a> <a class="btn btn-success w-25 mx-3" href="signup" role="button">傾向チェック</a> </div> </div> <div class="row py-3 justify-content-center"> <div class="card" style="width:calc(50% - .25rem)"> <div class="card-body"> <table class="table table-bordered"> <thead class="thead-dark"> <tr> <th class="text-center"><%= Payment.human_attribute_name(:created_at) %></th> <th class="text-center"><%= Payment.human_attribute_name(:item) %></th> <th class="text-center"><%= Payment.human_attribute_name(:price) %></th> <th class="text-center"><%= Payment.human_attribute_name(:category) %></th> <th></th> <th></th> </tr> </thead> <% @payments.each do |payment| %> <tr> <td class="text-center"><%= payment.created_at.strftime('%Y/%m/%d') %></td> <td class="text-center"><%= payment.item %></td> <td class="text-right"><%= payment.price %></td> <td class="text-center"><%= payment.category %></td> <td class="text-center"><%= link_to '編集', edit_payment_path(payment) %></td> <td class="text-center"><%= link_to '削除', payment, method: :delete, data: { confirm: "本当に削除しますか?"} %></td> </tr> <% end %> </table> <div class="row justify-content-center"> <%= paginate @payments %> </div> </div> </div> </div>
payments_helper.rb
module PaymentsHelper def this_month Date.today end def payments_this_month @payments_this_month = current_user.payments.where(created_at: this_month.in_time_zone.all_month) end def sum_payment_this_month sum = 0 payments_this_month @payments_this_month.each { |n| sum += n.price} sum end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/13 05:06 編集
2019/08/13 05:21
2019/08/13 05:27