コメントと添付ファイルを追加する部分を含むバグの表示/編集フォームがあります。部分的なフォームのページでのレンダリングの検証エラーメッセージ
ユーザーが無効な添付ファイルタイプまたは空のコメントを送信しようとすると、検証でエラー[validates :comment, presence: true]
と添付ファイル[using paperclip gem
]と同様のエラーが発生するため、同じバグにエラーメッセージを表示します。形。私のショー形式でGitHub Link
@私はここにすべてのヘルプを
show.html.erb
<%= render partial: 'bug_attachments/bug_form' %>
<%= render partial: 'comments/form' %>
comments/form.html.erb
<% @comment = Comment.new %>
<%= form_for [@bug, @comment] do |f| %>
<%= f.text_area :body, placeholder: "Add a comment" %>
<%= f.submit 'Add Comment'%>
<% end %>
comments_controller.rb
class Bugs::CommentsController < ApplicationController
before_action :authenticate_user!
before_action :set_bug
def create
@comment = @bug.comments.new comment_params
@comment.user = current_user
if @comment.save
redirect_to bug_path(@comment.bug_id), notice: 'Comment added successfully'
else
# Here I am setting @bug = 1 since when the user posts the comments, the @bug becomes nil.
@bug = Bug.find(1)
render 'bugs/show'
# redirect_to @comment, alert: 'Unable to save your comment'
end
end
private
def set_bug
@bug = Bug.find(params[:bug_id])
end
def comment_params
params.require(:comment).permit(:body)
end
end
を持っているで
レポはしてくださいますか?
これはフォームにajaxを追加することで実現します。 'remote:true' – mrvncaragay
@ Marv-C - これをどのように達成できるか教えてください。 – HSD