【问题标题】:Rails and ajaxForm - how to detect exceptionRails 和 ajaxForm - 如何检测异常
【发布时间】:2012-06-06 17:44:42
【问题描述】:

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

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

找到其他参考资料:

【问题讨论】:

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


    【解决方案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 如果它的死尝试缓存

    【讨论】:

    • 仍然无法以任何方式捕获异常消息。
    猜你喜欢
    • 2020-05-20
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2020-06-09
    • 2010-11-14
    • 2014-05-06
    • 2020-08-27
    • 2019-07-24
    相关资源
    最近更新 更多