2016-07-22 2 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 

이에

의 repo 제발입니까?

+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

관련 문제