teratail header banner
teratail header banner
質問するログイン新規登録

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

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

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

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

Q&A

1回答

1318閲覧

ActiveRecord::RecordNotFound エラー Couldn't find Motivation with 'id'=5

M.Y

総合スコア10

Ruby on Rails 6

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

0グッド

0クリップ

投稿2022/04/03 06:27

0

0

前提・実現したいこと

Railsでコメント機能を実装しており、投稿機能の詳細ページにコメントを表示させようとしています。
コメントを投稿した際のエラーメッセージです。
privateメゾット内のset_motivationで、投稿のparamsを取得し、showアクションで@commentsに代入しています。

発生している問題・エラーメッセージ

“`

ActiveRecord::RecordNotFound in MotivationsController#show Couldn't find Motivation with 'id'=5 Extracted source (around line #56): def set_motivation @motivation = Motivation.find(params[:id]) end Application Trace | Framework Trace | Full Trace app/controllers/motivations_controller.rb:56:in `set_motivation' Request Parameters: {"id"=>"5"} “` ### 該当のソースコード “` lass Motivationsコントローラー MotivationsController < ApplicationController before_action :move_to_index, except: [:index, :show] before_action :set_motivation, only: [:show, :edit, :update, :destroy] 中略 def create @motivation = Motivation.new(motivation_params) if @motivation.save redirect_to root_path else render :new end end def show @comment = Comment.new @comments = @motivation.comments.includes(:user) end 中略 private def motivation_params params.require(:motivation).permit(:title, :image, :purchase_date, :category_id, :person, :comment, :intuition, :purpose).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? redirect_to action: :index #サインインしていない場合、indexアクションに戻る end end def set_motivation @motivation = Motivation.find(params[:id]) end end

マイグレーションファイル(Comment)

class CreateComments < ActiveRecord::Migration[6.0] def change create_table :comments do |t| t.integer :user_id t.integer :motivation_id t.text :text, null: false t.timestamps end end end

マイグレーションファイル(CreateMotivations)

class CreateMotivations < ActiveRecord::Migration[6.0] def change create_table :motivations do |t| t.integer :user_id t.string :title, null: false t.date :purchase_date, null: false t.integer :category_id, null: false t.text :person t.text :comment t.text :intuition t.text :purpose, null: false t.timestamps end end end

Commentモデル

class Comment < ApplicationRecord belongs_to :motivation, optional: true belongs_to :user, optional: true validates :text, presence: true end

Motivationモデル

class Motivation < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions validates :category_id, numericality: { other_than: 1 } has_many :notifications belongs_to :user, optional: true belongs_to :category has_one_attached :image has_many :comments validates :title, presence: true validates :purchase_date, presence: true validates :purpose, presence: true validates :image, presence: true validates :category_id, numericality: { other_than: 1 } end

“`

試したこと

・エラー文を見るとidの値は入っているので、idがなぜか引き継がれていない?と思い、アソシエーションやテーブルを確認しました。
・set_motivationを(params[:id])ではなく,(motivation_params[:id])に変えたところ、以下のようなエラーになりました

param is missing or the value is empty: motivation

params.require(:motivation).permit(:title, :image, :purchase_date, :category_id, :person, :comment, :intuition, :purpose).merge(user_id: current_user.id) のところですね。

・id=5 となっていますが、{"id"=>"5"}なので、idを文字列でとってしまっているのが問題?とも思いました

わかりにくいかもしれませんが、回答いただけると大変助かります。よろしくお願いします。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

それは Motivation に id=5 のデータが無いからです。
その 5 というidはどの様に設定してものですか?

show を呼び出している viewとそれを表示している ACTION を載せていただくと問題がはっきりすると思います

投稿2022/04/03 14:23

winterboum

総合スコア23653

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

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

M.Y

2022/04/04 02:57

ご回答いただきありがとうございました。 ご回答を参考にさせていただき、show/html.erbを確認したところ、解決することができ無事コメントが保存され表示することができました。 以前までは <%= form_with(model: [@motivation, @comment], url: motivation_comments_path(@motivation.id), local: true) do |f| %> と記述していましたが、(@motivation.id)を無くしたところうまくいきました。 formで入力した情報をどこにどのように届けるかを指定するのに、idを取得するような書き方をしてしまったということでしょうか。 form_withのところの理解が甘かったです。 ご回答していただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.30%

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

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

質問する

関連した質問