2012-12-18 2 views
8

양식을 원격으로 변경했으며 양식이 작동하는 동안 오류가 있으면 오류 메시지가 더 이상 표시되지 않습니다.js 양식 레일을 사용하여 오류 메시지 렌더링

<%= render 'shared/error_messages' %> 

메시지가 다시 표시되도록하려면 좋은 착용감이 있습니까?

아래

내 컨트롤러는 ...

당신을 감사합니다.

respond_to do |format| 
    if @post.save 
    format.js { render :js => "window.location = '#{edit_post_path @post}'" } 
    format.html { redirect_to [:edit, @post] } 
    else 
    format.js { render :js => @post.errors } 
    format.html { redirect_to '/', :error => "Could not save comment" }  
    end 
end 

답변

12
respond_to do |format| 
    if @post.save 
    format.js { render :js => "window.location = '#{edit_post_path @post}'" } 
    format.html { redirect_to [:edit, @post] } 
    else 
    format.js { } 
    format.html { redirect_to '/', :error => "Could not save comment" }  
    end 
end 

# update.js.erb 

$(document).find("form").prepend('<%= escape_javascript(render("shared/error_messages", :formats => [:html])) %>'); 
+0

당신을 감사합니다! 그게 효과가있어 :) – user749798

+0

모델이 저장되었는지 여부를 처리하려고하는 update.js.erb와 같은 다른 조건이 있으면 고집하기 시작하면 조금 엉성해진다. – Donato

관련 문제