前提・実現したいこと
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/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/04 02:57