使用js form rails呈现错误消息

时间:2012-12-18 00:40:57

标签: ruby-on-rails ajax ruby-on-rails-3 forms error-handling

我将表单更改为远程,当表单现在可以正常工作时,如果出现错误,则不再显示错误消息。

<%= 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

1 个答案:

答案 0 :(得分:13)

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