アプリ:各地の名産品をレビューする
やりたいこと
商品ごとにレビュー数をカウントする
コード
application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery with: :exception include SessionsHelper private def require_user_logged_in unless logged_in? redirect_to login_url end end def counts(hokkaido) @count_hokkaido_reviews = hokkaido.hokkaido_reviews.count end def counts(touhoku) @count_touhoku_reviews = touhoku.touhoku_reviews.count end def counts(kantou) @count_kantou_reviews = kantou.kantou_reviews.count end end
hokkaidous_controllerのshowアクション
def show @hokkaido = Hokkaido.find(params[:id]) @hokkaido_reviews = @hokkaido.hokkaido_reviews.order('created_at DESC').page(params[:page]) counts(@hokkaido) end
touhokus_controllerのshowアクション
def show @touhoku = Touhoku.find(params[:id]) @touhoku_reviews = @touhoku.touhoku_reviews.order('created_at DESC').page(params[:page]) counts(@touhoku) end
kantous_controllerのshowアクション
def show @kantou = Kantou.find(params[:id]) @kantou_reviews = @kantou.kantou_reviews.order('created_at DESC').page(params[:page]) counts(@kantou) end
問題点
レビュー数のカウントが正常に機能しない
--具体例--
hokkaidos/showにアクセスすると以下のエラーが発生する
https://gyazo.com/0dbe931258296c3a5f8628422f8d7cc3
touhokus/showにアクセスすると以下のエラーが発生する
https://gyazo.com/a6ab26f502c56ac04cf86a66264a0d6f
kantous/showにアクセスしたときはエラーが発生しません。
https://gyazo.com/accd029d46f4d9e68142176dc83cf73f
ということは、application_controller.rb
に記載されている通り、一番下の群にある、
def counts(kantou) @count_kantou_reviews = kantou.kantou_reviews.count end
が優先しているため、関東の商品のみ、レビュー数のカウント機能が正常に働いていると思います。
これを解決し、北海道・東北それぞれの商品のレビュー数のカウント機能も正常に働かせるにはどうすればいいでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/04 04:56