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

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

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

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

4853閲覧

【Ruby on Rails】同じviewにrenderした時に、エラーメッセージが表示できない

is02

総合スコア17

Ruby on Rails 5

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

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2020/01/31 09:36

前提・実現したいこと

コメント機能をつけております。
空白の状態でコメントしようとしたときに、エラーメッセージが表示されるようにしたいです。

起きている問題

現状は、空白の状態でコメントしようとしたときにバリデーションはかかっていると思うのですが、エラーメッセージが表示されません。
恐らく、post_comments_controllerで@post_comment = PostComment.newを記載しているため、バリデーション情報が上書きされているのでエラーメッセージが表示されないんだと思います。

しかし、@post_comment = PostComment.newを消してしまうと下記のエラーコードが表示されるため、どのように書けばいいのか分かりません。

イメージ説明

該当のソースコード

####モデル
post_image.rb

class PostImage < ApplicationRecord belongs_to :user has_many :favorites, dependent: :destroy has_many :fav_users, through: :favorites, source: :user has_many :cosplay_favorites, dependent: :destroy has_many :cosplay_fav_users, through: :cosplay_favorites, source: :user has_many :post_comments, dependent: :destroy # refile定義 attachment :real_image attachment :cosplay_image # 投稿を降順に並び替えし、最新のものを上にくるようにする default_scope -> { order(created_at: :asc) } end

post_comment.rb

class PostComment < ApplicationRecord belongs_to :user belongs_to :post_image # コメントが空白の時はエラー表示させる validates :comment, presence: true end

####コントローラー
post_images_controller.rb

class PostImagesController < ApplicationController def new @post_image = PostImage.new end # 投稿データの保存 def create @post_image = PostImage.new(post_image_params) @post_image.user_id = current_user.id @post_image.save redirect_to post_images_path end def index @post_images = PostImage.page(params[:page]).reverse_order end def show @post_image = PostImage.find(params[:id]) @post_comment = PostComment.new end def destroy @post_image = PostImage.find(params[:id]) @post_image.destroy redirect_to post_images_path end private # 投稿データのストロングパラメータ def post_image_params params.require(:post_image).permit(:real_image_name, :cosplay_image_name, :real_image, :cosplay_image, :caption, :favorites_count) end end

post_comments_controller.rb

class PostCommentsController < ApplicationController def create post_image = PostImage.find(params[:post_image_id]) comment = current_user.post_comments.new(post_comment_params) comment.post_image_id = post_image.id if comment.save redirect_to post_image_path(post_image) else @post_image = PostImage.find(params[:post_image_id]) @post_comment = PostComment.new render 'post_images/show' end end private def post_comment_params params.require(:post_comment).permit(:user_id, :post_image_id, :comment) end end

####ビュー
post_images/show.html.erb

<div class="header"> <nav class="navigation"> <img src="/assets/logo.png"> <ul> <li> <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> </li> <li> <%= link_to '投稿一覧', root_path %> </li> <li> <%= link_to 'マイページ', user_path(current_user.id) %> </li> </ul> </nav> </div> <div class="post_images_show_wrapper"> <div class="index_box"> <div class="post_images_index_user"> <ul> <li> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> </li> <li><p><%= link_to "#{@post_image.user.name}", user_path(@post_image.id) %></p></li> </ul> </div> <div class="post_images_index_title"> <div class="image_title"> <h2>Real</h2> </div> <div class="image_title"> <h2>Cosplay</h2> </div> </div> <div class="post_images_box"> <div class="post_image"> <%= attachment_image_tag @post_image, :real_image %> </div> <div class="post_image"> <%= attachment_image_tag @post_image, :cosplay_image %> </div> </div> <div class="image_name"> <%= @post_image.real_image_name %> </div> <div class="image_name"> <%= @post_image.cosplay_image_name %> </div> <div class="favorites_area"> <div class="favorite_area"> <%= render partial: 'post_images/post_images', locals: { post_image: @post_image } %> </div> <div class="favorite_area"> <%= render partial: 'post_images/cosplay_post_images', locals: { post_image: @post_image } %> </div> </div> <div class="image_caption"> <ul> <li> <%= attachment_image_tag @user, :profile_image, fallback: "no_image.jpg" %> </li> <li> <p><%= @post_image.user.name %> </p> </li> <li> <p><%= @post_image.post_comments.count %>件のコメント</p> </li> <li> <% if @post_image.user == current_user %> <p> <%= link_to "投稿を削除", post_image_path(@post_image), method: :delete %></p> <% end %> </li> </ul> <span class="caption"><%= @post_image.caption %></span> </div> <p class="comment_title">みんなのコメント</p> <div class="comment_area"> <% @post_image.post_comments.each do |post_comment| %> <div class="comment_box"> <div class="comment_user"> <span class="date"><%= post_comment.created_at.strftime('%Y/%m/%d') %> </span> <span class="user"><%= post_comment.user.name %> : </span> </div> <div class="comment_text"> <%= post_comment.comment %> </div> </div> <% end %> </div> <% if @post_image.errors.any? %> <% @post_image.errors.full_messages.each do |message| %> <li class="edit_error"><%= message %></li> <% end %> <% end %> <div class="comment_type"> <%= form_for [@post_image, @post_comment] do |f| %> <ul> <li><%= f.text_area :comment, placeholder: "コメントを入力してください" %></li> <li><%= f.submit "送信する" %></li> </ul> <% end %> </div> </div> </div>

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

ruby 2.5.7p206
Rails 5.2.4.1

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

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

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

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

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

guest

回答1

0

ベストアンサー

createの書き方です

@post_image.save redirect_to post_images_path

ではなく

if @post_image.save redirect_to post_images_path] else render action: :new end

としてください。

追記

def create @post_image = PostImage.find(params[:post_image_id]) @post_comment = current_user.post_comments.new(post_comment_params) @post_comment.post_image_id = post_image.id if @post_comment.save redirect_to post_image_path(@post_image) else render 'post_images/show' end end

投稿2020/01/31 10:33

編集2020/01/31 14:06
winterboum

総合スコア23376

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

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

is02

2020/01/31 10:42 編集

回答ありがとうございます。 同じように書いてみましたが、エラー内容は変わりません。 アドバイスを頂きたいです。
winterboum

2020/01/31 10:45

newのviewを2回使うことになりますが、最初の new の時ですか?そのエラーは
is02

2020/01/31 11:35

すみません、newのviewというのが理解できておりません。 ルーティングを見てもnewのviewは作っていないはずなんですが、 このエラーは、post_comments_controllerのcreateアクション内、 if comment.save redirect_to post_image_path(post_image) else @post_image = PostImage.find(params[:post_image_id]) render 'post_images/show' end という文を追加したところエラーが発生しました。 話をうまく汲み取れず申し訳ありません。
winterboum

2020/01/31 11:52

はて、 def new @post_image = PostImage.new end はどのviewを使ってますか?
is02

2020/01/31 11:57 編集

post_images/new.html.erbで使っておりました。 <div class="post_images_new_wrapper"> <h2>画像を投稿する</h2> <%= form_for(@post_image, url: post_images_path) do |f| %> <%= f.text_field :real_image_name, class: "post_images_title", placeholder:"日常画像のタイトル" %> <%= f.attachment_field :real_image, class: "post_images_upload" %> <%= f.text_field :cosplay_image_name, class: "post_images_title", placeholder:"コスプレ画像のタイトル" %> <%= f.attachment_field :cosplay_image, class: "post_images_upload" %> <%= f.text_area :caption, placeholder:"コメントを入力してください" %> <%= f.submit "送信" %> <% end %> </div> ここは先ほどの文を追加する前も画像等送れております。
winterboum

2020/01/31 12:01

すみません、すると質問のタイトルの「同じviewにrenderした時に」が??? action new → vew new → action create と来て、その次 validationでコケた時にどのviewで表示するのですか?
winterboum

2020/01/31 12:05

え? url: post_images_path ですか? これどのcontroller のどの action にどのmethodで飛んでます?  Railsの通常の設定ですと post_images_path は post_image コントローラーのindex に get で行くためのものですが
is02

2020/01/31 12:12

間違っていたらすみません。 ご指摘頂いている箇所はpost_images/new.html.erbで画像等を送った時の挙動だと思っています。 post_images/show.html.erb <%= form_for [@post_image, @post_comment] do |f| %> <ul> <li><%= f.text_area :comment, placeholder: "コメントを入力してください" %></li> <li><%= f.submit "送信する" %></li> </ul> <% end %> post_comments_controller def create post_image = PostImage.find(params[:post_image_id]) comment = current_user.post_comments.new(post_comment_params) comment.post_image_id = post_image.id if comment.save redirect_to post_image_path(post_image) else @post_image = PostImage.find(params[:post_image_id]) @post_comment = PostComment.new render 'post_images/show' end end で、保存できずバリデーションにひっかかったときに同じ画面のpost_images/show.html.erbに飛ばそうと思っていて、同時にバリデーションのエラーメッセージを表示させたいと考えています。 現在の問題点は、post_images/show.html.erbでコメントを送ろうとした時にpost_comments_controllerで処理しているものだと考えておりました。 その際に、@post_comment = PostComment.newを書かないとエラーが出てしまったので質問させていただたいた次第です。
is02

2020/01/31 12:13

ルーティングをもう一度見直してみます。
winterboum

2020/01/31 14:04

そのshowのviewから<%= form_for [@post_image, @post_comment] でデータを送る先が post_comments_controller の create でそこでのエラーが表示できない。 newでのcreateではない、 ですね? elseで@post_imageなどを作りなおしてしまうのでエラー情報が消えてしまいます ここだとインデントつかなくて見難いので回答に追記します
is02

2020/02/01 02:57 編集

def create @post_image = PostImage.find(params[:post_image_id]) @post_comment = current_user.post_comments.new(post_comment_params) @post_comment.post_image_id = @post_image.id if @post_comment.save redirect_to post_image_path(@post_image) else render 'post_images/show' end end と記載して、view画面のpost_images/showの <% if @post_comment.errors.any? %> <% @post_comment.errors.full_messages.each do |message| %> <li class="edit_error"><%= message %></li> <% end %> <% end %> にしたら無事表示されました!ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問