質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1280閲覧

Railsで月次ページの作成方法

minipe555

総合スコア18

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/08/13 03:58

編集2019/08/13 04:34

質問の背景

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

def payments_this_month が見当たりませんが、、、、

def index month = params[:month] || Date.today @payments = Payment.where(date: month.all_month).   page(params[:page]).per(PER) end``` 辺りでは。 view index で月を変える仕掛けはお任せ。

投稿2019/08/13 04:40

winterboum

総合スコア23284

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

minipe555

2019/08/13 05:06 編集

回答、ありがとうございます。 すみません。helper追記しました。(def payments_this_month) ``` month = params[:month] || Date.today ``` indexページにくる時に、月の値を送信させるような感じですよね? >view index で月を変える仕掛けはお任せ。 → ここにどんな方法があるのか、知りたいです、、、 月毎にページ用意する感じでしょうか。 それとも、indexページ一つで月を変更する仕組みも、ありえるのでしょうか。
winterboum

2019/08/13 05:21

何ヶ月も先に飛ばすのでなければ <%= link_to "前月",payments_path(month: @month.prev_month) %> でしょうか。 def index で month を @month に直しておいて。 あ、 month = params[:month] || Date.today だとエラーになるかな @month = parse(params[:month] ? Date.parse(params[:month) : Date.today かな。
minipe555

2019/08/13 05:27

高度なテクニックに衝撃です。 解読します。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問