前提・実現したいこと
コメント機能をつけております。
空白の状態でコメントしようとしたときに、エラーメッセージが表示されるようにしたいです。
起きている問題
現状は、空白の状態でコメントしようとしたときにバリデーションはかかっていると思うのですが、エラーメッセージが表示されません。
恐らく、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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/31 10:42 編集
2020/01/31 10:45
2020/01/31 11:35
2020/01/31 11:52
2020/01/31 11:57 編集
2020/01/31 12:01
2020/01/31 12:05
2020/01/31 12:12
2020/01/31 12:13
2020/01/31 14:04
2020/02/01 02:57 編集