1

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:

Community
  • 1
  • 1
Paul
  • 25,812
  • 38
  • 124
  • 247

1 Answers1

1

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

Sully
  • 14,672
  • 5
  • 54
  • 79