いつもお世話になっています。
現在、私はRailsでコメント機能を実装しており、ユーザーがコメントを入力した後に「コメントをする」ボタンを押すと、コメントができるようにしています。
しかし、もし、ユーザーが何もコメントを入力せずに「コメントをする」ボタンを押したときには、「コメントを入力してください」等のエラー文を表示させたいのですが、
param is missing or the value is empty: comment
と言うエラーが出てしまい、上手くいきません。
現在のコメント機能に関するコードは、以下の様になっています。
#comments_controller.rb class CommentsController < ApplicationController def create @article= Article.find_by(replaced_token:params[:article_replaced_token]) @comment = @article.comments.build(comment_params) @comment.user_id = current_user.id @comment.save if @comment.save flash[:notice] = "コメントが正常に投稿されました。" redirect_to article_path(@article) else render new end end private def comment_params params.require(:comment).permit(:body) end end
#models/comment.rb class Comment < ApplicationRecord validates :body, presence: true validates :article_id, presence: true validates :user_id, presence: true belongs_to :user belongs_to :article end
#articles/show.html.erb <h3>コメントを残す</h3> <%= form_for [@article, @article.comments.build] do |f| %> <%= f.text_area :body %> <%= f.submit 'コメントを投稿する' %> <% end %>
よろしくお願い致します。
あなたの回答
tips
プレビュー