不具合
Dashboards#index
前月へのリンクをクリックすると、/dashboardへリダイレクトされる。
原因
Started GET "/dashboard/2019-12" vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98 Processing by DashboardsController#index as HTML Parameters: {"year_month"=>"2019-12"} Redirected to http://localhost:3000/dashboard Completed 302 Found in 29499ms (ActiveRecord: 0.4ms)
GET /dashboard/2019-12
した後に、/dashboard
にリダイレクトされている。
Dashboardコントローラを確認する。
# Dashboard#index def index @diary = current_user.diaries.build if params[:year_month].nil? .... elsif Date.strptime(params[:year_month], '%Y-%m').month < Date.today.month .... else redirect_to '/dashboard' end end #
リクエストのパラメータに:year_month
が含まれる場合に、リクエストに含まれる:year_month
パラメータの月にあたる数値と現在の月の数値を比べ、リクエストした年月の数値の方が小さかった場合に、前月のレコードを検索して返す処理を記述していた。
年が変わって、12月のレコードが 12 < 1 でfalseとなってしまい、else節が実行されてしまうようになっていた。(なんでこんなこと書いたのか覚えてない…)
対処
# Dashboards#index ... elsif params[:year_month] ....
とした。
結果
月ごとのレコードが表示される様になった。