2012-12-18 16 views
8

Zmieniłem formularz na zdalny, a gdy formularz działa, komunikaty o błędach nie są już wyświetlane, jeśli wystąpił błąd.renderowanie komunikatów o błędach z szynami JS

<%= render 'shared/error_messages' %> 

Czy jest dobry sposób na ponowne wyświetlenie wiadomości?

poniżej to mój kontroler ...

dziękuję.

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 

Odpowiedz

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

dziękuję! to działało :) – user749798

+0

Staje się trochę zaniedbany, gdy zaczynasz się kłuć, jeśli w innych warunkach w coś takiego jak update.js.erb próbuje poradzić sobie z tym, czy model został zapisany, czy nie – Donato

Powiązane problemy