简体   繁体   中英

Rails and ajaxForm - how to detect exception

I made a standard Rails form and then wrapped it into JQ ajaxForm. Now the success callback is called in any case - whether an Rails exception occured while submitting or not.

How to make ajaxForm detect Rails exception? I want in case of error to remain in the form and display exception text.

Additional references found:

Check the request returned status to determine if there is an error or just do it like

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

Edit: Try this

"A block of code to run if an error occurs"

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

For one method

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

For the whole app

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 if its dead try cached

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM