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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1273閲覧

Railsで閲覧履歴を表示する。browsing_histories

rayi0630

総合スコア9

Ruby on Rails 5

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

Ruby on Rails

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

0グッド

1クリップ

投稿2020/02/25 15:13

##閲覧履歴の表示が上手く出来ない。

現在小説サイトの真似をし練習中です。閲覧履歴を見れると良いと思い。
https://qiita.com/solare_tech/items/3cad7ada6c14a00ee15c
こちらのサイトを参考にして現在作っています。

####テーブル
・novel_lists
・users
・browsing_histories

モデルの関係性は下記となります。

■class User < ApplicationRecord has_many :browsing_histories, dependent: :destroy ■class NovelList < ApplicationRecord has_many :browsing_histories, dependent: :destroy ■class BrowsingHistory < ApplicationRecord belongs_to :user belongs_to :novel_list

###表示したいページ

novel_list_historyというページを別で作り、こちらに閲覧履歴一覧を表示したいと思ってます。 novel_listsの閲覧履歴全てなので、collectionで良いと思いcollectionとしています。 resources :novel_lists, only: [:index, :show, :new, :create, :edit, :update, :destroy] do collection do get :novel_list_history end

###コントローラ

class NovelListsController < ApplicationController def show @novel_list = NovelList.find(params[:id]) @novel_posts = @novel_list.novel_posts #ここから閲覧履歴を保存するコード new_history = @novel_list.browsing_histories.new new_history.user_id = current_user.id #if文追加、ログイン中のユーザー(current_user)の閲覧履歴(browsing_histories)の中で記事ID(article_id)が #URLに入力されているID(params[:id])と同じものがすでに存在しているか?(exists?) if current_user.browsing_histories.exists?(novel_list_id: "#{params[:id]}") old_history = current_user.browsing_histories.find_by(novel_list_id: "#{params[:id]}") old_history.destroy end new_history.save #ここから同一ユーザーの閲覧履歴の件数が上限を超えたときの処理と上限設定 histories_stock_limit = 10 histories = current_user.browsing_histories.all if histories.count > histories_stock_limit histories[0].destroy end end

####showに保存する処理を書きました。

続いて、novel_list_historyメソッドを作り、こちらで全てのデータを取得し。

def novel_list_history #閲覧履歴 @history = BrowsingHistory.all end
novel_lists/novel_list_histry.html.erb <% @history.each do |history| %> <%= history.novel_list.title %> <% end %> ここまでは表示に成功しています。 きちんとタイトルが表示されました。 リンクにしたいので、 novel_list GET /novel_lists/:id(.:format) こちらへのpathを書きます。 <% @history.each do |history| %> <%= link_to history.novel_list.title, novel_lost_path(history) %> <% end %> ここで、リンクへと飛ぶとエラーが出て表示さきへ飛べませんでした。

###エラー内容
ActiveRecord::RecordNotFound in NovelListsController#show
Couldn't find NovelList with 'id'=#BrowsingHistory::ActiveRecord_Relation:0x00007fa9209e4550
Extracted source (around line #9):

def show
@novel_list = NovelList.find(params[:id]) ←エラー箇所
@novel_posts = @novel_list.novel_posts

###試した事 BrowsingHistoryのidと関連させれていないのだと思い、 @novel_histry = @novel_list.browsing_historiesを追加。 変化はなく、エラーは変わりません。 @novel_list = NovelList.find(params[:id]) ←を追加し、pathに渡してるデータを@novel_listにしようとしたのですが、 こちらも、エラー変わらずでした。 その他関連しそうな部分を変えてみて様子をみているのですが、エラー内容は変わらず、苦戦しております。 わかる方いらっしゃいましたら、よろしくお願いします。

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

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

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

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

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

hatsu

2020/02/25 15:33

rails routesの結果があれば載せていただけると嬉しいです! `only: [:index, :show, :new, :create, :edit, :update, :destroy]`はアクション全て書いてためonlyの意味をなしていないので、不要かもしれないです。 またnovel_lost_path(history.id)にした結果をあると幸いです。
rayi0630

2020/02/27 04:37 編集

ご回答ありがとうございます。 ``` novel_list_history_novel_lists GET /novel_lists/novel_list_history(.:format) novel_lists#novel_list_history  novel_lists GET /novel_lists(.:format) novel_lists#index POST /novel_lists(.:format) novel_lists#create new_novel_list GET /novel_lists/new(.:format) novel_lists#new edit_novel_list GET /novel_lists/:id/edit(.:format) novel_lists#edit novel_list GET /novel_lists/:id(.:format) novel_lists#show PATCH /novel_lists/:id(.:format) novel_lists#update PUT /novel_lists/:id(.:format) novel_lists#update DELETE /novel_lists/:id(.:format) novel_lists#destroy novel_posts GET /novel_posts(.:format) ``` こちらが、novel_list_historyとnovel_listのrails routes結果となります。 ■<% @history.each do |history| %> <%= link_to history.novel_list.title, novel_list_path(history.id) %> <% end %> こちらにしてみて、リンクを踏んだのですが、 ``` ActiveRecord::RecordNotFound in NovelListsController#show Couldn't find NovelList with 'id'=12 Extracted source (around line #9): def show @novel_list = NovelList.find(params[:id])ここにエラーが出ました。 @novel_posts = @novel_list.novel_posts ``` このような状況となります。
rayi0630

2020/02/27 04:39

解決する事が出来ました。 ありがとうございます。
guest

回答1

0

ベストアンサー

ruby

1 @novel_list = NovelList.find(params[:id])

気になるのは、以下です。
1.params[:id]の中身は整数ではない。
(hatsuさんのご指摘どおり、view側でnovel_lost_path(history.id)とするとか必要かも。
2.整数である場合、idが存在しない.
find()だと1件もヒットしない場合、エラーになるので、find_by()とか使う。

投稿2020/02/26 01:21

mongolia

総合スコア133

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

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

rayi0630

2020/02/27 04:37

ご回答ありがとうございます。 hatsuさんに言われた通りの、history.idにして。 mongoliaさんのいう通り、find_byに変更したらlinkを上手く飛ぶ事が出来ました。 ありがとうございます。 今回、整数ではない値を渡していたので、特定できずに、エラーとなっていたという事でしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問