1

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

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

发现的其他参考资料:

4

1 回答 1

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如果它的死尝试缓存

于 2012-06-06T17:56:27.553 回答