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

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

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

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

Q&A

解決済

1回答

1354閲覧

コメント投稿ページでコメントの保存ができません。

moto12

総合スコア15

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/03 12:04

編集2020/11/03 14:09

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

roomの中でメッセージ投稿を行うことができ、そのメッセージをクリックすると、コメント投稿ページに遷移します。
そこで、コメントを投稿しようとすると、以下のエラーメッセージが出ます。

どのようにすれば、コメントが正常に保存されますでしょうか。

エラーメッセージ ActiveRecord::RecordNotFound in CommentsController#index Couldn't find Message without an ID

該当のソースコード

Ruby

1【comments.controller.rb】 2 3class CommentsController < ApplicationController 4 5 def index 6 @messages = Message.find(params[:id]) 7 @room = Room.find(params[:room_id]) 8 @comments = @messages.comments.include(:user) 9 end 10 11 def new 12 @comment = Comment.new 13 @message = Message.new 14 @room = Room.find(params[:room_id]) 15 @messages = @room.messages.includes(:user).order("created_at DESC") 16 end 17 18 def create 19 if @comment = Comment.create(comment_params) 20 redirect_to root_path 21 22 end 23 end 24 25 private 26 def comment_params 27 params.require(:comment).permit(:text).merge(user_id: current_user.id, message_id: params[:message_id], room_id: params[:room_id]) 28 end 29end 30

Ruby

1【index.html.erb (comment)2 3<div class="comment"> 4 <div class="comment-box"> 5 <%= form_with model: @comment,url:room_message_comments_path(@room, @messages),local: true do |f| %> 6 <%= f.text_area :text, placeholder: "コメントする", class: "comment-text" %> 7 <p class="comment-warn"> 8 相手のことを考え丁寧なコメントを心がけましょう。 9 <br> 10 不快な言葉遣いなどは利用制限や退会処分となることがあります。 11 </p> 12 <%= f.submit "コメントする"%> 13 <% end %> 14 </div>

Ruby

1【messages.controller.rb】 2 3class MessagesController < ApplicationController 4 def index 5 @comment = Comment.new 6 @message = Message.new 7 @room = Room.find(params[:room_id]) 8 @messages = @room.messages.includes(:user).order("created_at DESC") 9 @users = @room.users.order("student_number ASC") 10 end 11 12 def new 13 @comment = Comment.new 14 end 15 16 def show 17 @comment = Comment.new 18 @comments = @message.comments.includes(:user) 19 end 20 21 def create 22 @room = Room.find(params[:room_id]) 23 @message = @room.messages.new(message_params) 24 if @message.save 25 redirect_to room_messages_path(@room) 26 else 27 @messages = @room.messages.includes(:user) 28 render :index 29 end 30 end 31 32 33 private 34 35 def message_params 36 params.require(:message).permit(:content, :image).merge(user_id: current_user.id) 37 end 38end 39 40

Ruby

1【routes.rb】 2 3Rails.application.routes.draw do 4 devise_for :users 5 get 'messages/index' 6 get 'comments/index' 7 root to: "rooms#index" 8 resources :users 9 resources :rooms do 10 resources :messages do 11 resources :comments 12 end 13 end 14end 15 16

Ruby

1【ログ】 2 3room_message_comments GET     /rooms/:room_id/messages/:message_id/comments(.:format) comments#index 4POST                 /rooms/:room_id/messages/:message_id/comments(.:format) comments#create 5new_room_message_comment GET /rooms/:room_id/messages/:message_id/comments/new(.:format) comments#new 6

Ruby

1【index.html.erb (message)2 3(省略) 4(comment.new.htmlへ遷移するためのパス部分) 5 6 <div class="message-content"> 7 <%=link_to message.content, new_room_message_comment_path(@room, @messages) %> 8 </div> 9 10(省略)

Ruby

1【エラーメッセージ画面のGyazo2https://gyazo.com/8b7f3ec749368907f777596b6ec6115d 3

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

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

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

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

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

guest

回答1

0

ベストアンサー

混乱していますね。
エラーメッセージからは CommentsController#index に params[:id] が渡っていないとわかります。
その原因を追いかけるには
「roomの中でメッセージ投稿を行うことができ、そのメッセージをクリックすると、コメント投稿ページに遷移します」
とありますが、載っているviewは【index.html.erb (comment)】です。
newではないですが、これで投稿するのですね?
すると CommentsControllerのindex を呼ぶviewとそれを呼び出すactionが
params[:id]を用意しているかどうか、が問題です。
それは 「roomの中でメッセージ投稿を行うことができ、そのメッセージをクリックすると、コメント投稿ページに遷移します」から判断すると roomでしょうか。
そのviewもcontrollerもないので、判断できません。

ということで、

  1. commentを入力するのは newでなくてindexであるのはまちがいないですか?
  2. CommentsControllerのindex を呼ぶviewとそれを呼び出すaction のcodeを載せてください

投稿2020/11/03 13:16

winterboum

総合スコア23347

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

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

moto12

2020/11/03 14:04

色々と勘違いしていましたので、コメント投稿ページをnew.html.erbに変更しましたが、コメント投稿ページのURLが明らかにおかしいような気がします。なぜこのような状況になっているのでしょうか。 この部分を解決しない限り、commentsテーブルに値が保存されないような気がします。 参考資料 Gyazo Gif https://gyazo.com/3a52d242556271e49cb86f9d63166a7b
winterboum

2020/11/03 22:35

コメント投稿ページのURL とは、「ページのURL」ですか?「ページに作られる遷移先のURL」ですか?
moto12

2020/11/04 01:16

ページのURLです。
winterboum

2020/11/04 01:56

それは、そのページを呼び出す viewがそうなっているのでは? commentのnewを呼ぶ部分のlinkはどうなってます?
moto12

2020/11/04 03:17

new_message_comment_path(@room, @messages) となっていたのを new_message_comment_path(@room) に変えたところうまくいきました ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問