2016-07-22 21 views
0

コメントと添付ファイルを追加する部分を含むバグの表示/編集フォームがあります。部分的なフォームのページでのレンダリングの検証エラーメッセージ

ユーザーが無効な添付ファイルタイプまたは空のコメントを送信しようとすると、検証でエラー[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 

を持っているで

レポはしてくださいますか?

+0

これはフォームにajaxを追加することで実現します。 'remote:true' – mrvncaragay

+0

@ Marv-C - これをどのように達成できるか教えてください。 – HSD

答えて

0

あなたがそうのようにそれを行うことができます(プロジェクトのために編集):

<% if @comment && @comment.errors.any? %> 
    <ul> 
    <% @comment.errors.full_messages.each do |error_message| %> 
    <li><%= error_message %></li> 
    <% end %> 
    </ul> 
<% end %> 

それはフラッシュである行う別の興味深い方法:

if @comment.errors.any? 
    flash[:error] = @comment.full_messages.join(', ') 
    redirect_to #somewhere 
else 
:あなたのコントローラで

+0

おかげさまで、 'flash [:errors]'はうまく動作します。 '@ comment.errorys.any'について私は試してみましたが、この@commentを実行している間は何も表示されず、エラーメッセージが表示されません。私の理解はリダイレクトであり、以前のエラーはリダイレクトでは持続せず、新鮮な要求です。 – HSD

+0

モデルにバリデーションを指定しましたか? – Alex

+0

はい、モデルのバリデーションを指定しました – HSD

関連する問題