Rails和ajaxForm - 如何检测异常

时间:2012-06-06 17:44:43

标签: ruby-on-rails exception-handling ajax-forms

我制作了一个标准的Rails表单,然后将其包装到JQ ajaxForm中。现在调用成功回调 - 无论是否在提交时发生Rails异常。

如何让ajaxForm检测Rails异常?我希望在出现错误的情况下保留在表单中并显示异常文本。

发现了其他参考资料:

1 个答案:

答案 0 :(得分:1)

检查请求返回状态以确定是否存在错误或仅执行

 if(jqXHR === 0 ){
     //do something with error
 } else {
     //success
 }

修改 试试这个

“发生错误时要运行的代码块”

$.ajaxSetup({
  error: function(res){
    $('body').prepend(res.responseText)
  }
});

对于一种方法

def new
  @item = Item.new
  rescue
    render 'shared/exception', :layout => 'overlay'
end

对于整个应用

class ApplicationController
  around_filter :xhr_exceptions
  # ...
  private
  def xhr_exceptions
    yield
    rescue
      render('shared/exception', :layout => 'overlay', :status => 500) if request.xhr?
  end
end

http://codequietly.com/blog/2010/08/rails-ajax-and-exceptions-bring-it-on如果它的死尝试缓存