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

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

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

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

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

Q&A

解決済

1回答

1170閲覧

comment投稿失敗時にエラーメッセージが出ない

moto12

総合スコア15

Ruby on Rails

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

バリデーション

Validationとは特定の入力データが、求められた条件に当てまっているかをチェックするために使われます。

0グッド

0クリップ

投稿2021/01/04 03:11

前提・実現したいこと

それぞれのメッセージに対して、コメントをできる実装の中で、コメント投稿失敗時にバリデーションによるエラーメッセージを表示させたいが、以下のメッセージが出る。メッセージ投稿時のエラーメッセージは問題なく出たのに、コメント投稿時だけ出るのはなぜでしょうか。

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

イメージ説明
エラーメッセージ

該当のソースコード

rails

1【comment new.html.erb】 2 3<div class="comment"> 4 <div class="comment-box"> 5 <%= form_with model: [@comment, @messages],url:message_comments_path(@message),local: true do |f| %> 6 <%= render 'layouts/error_messages', model: f.object %> 7 <%= f.text_area :text, placeholder: "質問する", class: "comment-text", id:"input" %> 8 <%= hidden_field_tag :message_id,@message.id %> 9 10少略 11

rails

1【_error_messages.html.erb】 部分テンプレートファイル 2 3<% if model.errors.any? %> 4<div class="error-alert"> 5 <ul> 6 <% model.errors.full_messages.each do |message| %> 7 <li class='error-message'><%= message %></li> 8 <% end %> 9 </ul> 10</div> 11<% end %>

log

1 2Started GET "/messages/2/comments/new" for ::1 at 2021-01-04 12:06:43 +0900 3Processing by CommentsController#new as HTML 4 Parameters: {"message_id"=>"2"} 5 User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 6 Message Load (0.4ms) SELECT `messages`.* FROM `messages` WHERE `messages`.`id` = 2 LIMIT 1 7 ↳ app/controllers/comments_controller.rb:10:in `new' 8 Rendering comments/new.html.erb within layouts/application 9 Message Load (0.2ms) SELECT `messages`.* FROM `messages` LIMIT 11 10 ↳ app/views/comments/new.html.erb:4 11 Rendered layouts/_error_messages.html.erb (Duration: 3.7ms | Allocations: 3303) 12 Rendered comments/new.html.erb within layouts/application (Duration: 4.0ms | Allocations: 3425) 13Completed 500 Internal Server Error in 10ms (ActiveRecord: 1.0ms | Allocations: 7513) 14 15 16 17ActionView::Template::Error (undefined method `errors' for #<Message::ActiveRecord_Relation:0x00007f89dc1d7230>): 18 1: <% if model.errors.any? %> 19 2: <div class="error-alert"> 20 3: <ul> 21 4: <% model.errors.full_messages.each do |message| %> 22 23app/views/layouts/_error_messages.html.erb:1 24app/views/comments/new.html.erb:4 25app/views/comments/new.html.erb:3 26 27

rails

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

rails

1【comment.rb】 2 3class Comment < ApplicationRecord 4 belongs_to :message 5 belongs_to :user 6 has_many :agrees, dependent: :destroy 7 has_many :responses, dependent: :destroy 8 has_many :agreed_users, through: :agrees, source: :user 9 10 validates :text, presence: true, length: {maximum: 75 } 11end 12

rails

1

試したこと

ここに問題に対して試したことを記載してください。

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

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

action new が edit の様に見えるのですが、、、、
さておき
form_with model: [@comment, @messages]

form_with model: [@comment, @message]
では?

投稿2021/01/04 04:49

winterboum

総合スコア23347

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問