TAGS :Viewed: 17 - Published at: a few seconds ago

[ render error messages with js form rails ]

I changed my form to remote, and while the form now works, the error messages are no longer displaying if there's an error.

<%= render 'shared/error_messages' %>

is there a good wear to get the messages to show again?

below is my controller...

thank you.

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

Answer 1


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])) %>');