title: Rails + ajaxForm: Use the 'error' callback when error uploading file
body: <h2>Background</h2>
  
  <p>I first wanted to upload a file via json and get a response in that way as well.</p>
  
  <p>I'm using:</p>
  
  <ul>
  <li>Rails 3</li>
  <li><a href="http://jquery.malsup.com/form" rel="nofollow noreferrer">ajaxForm</a></li>
  </ul>
  
  <p>I soon found out that <a href="https://stackoverflow.com/questions/2755802/jquery-ajaxform-returning-json-file/2756314#2756314">you can't get a reponse in json</a>. So, I follow that advice and returned as text.</p>
  
  <p>I'm able to get things working after removing the pre tags. Ugly solution, but it's an ugly problem.</p>
  
  <h1>Problem</h1>
  
  <p>Now, my problem is <strong>handling errors</strong>.</p>
  
  <h2>Here's the JS:</h2>
  
  <pre><code>$('form#new_image').submit(function() {
    $(this).ajaxSubmit({
      dataType: 'text',
      beforeSubmit: showLoading,
      success: imageUploadSuccess,
      error: imageUploadError
    });
    return false;
  });
  
  function imageUploadSuccess(data) {
    var jsonObject = $.parseJSON((/&lt;pre&gt;(.+)&lt;\/pre&gt;/.exec(data))[1]);
    //Do something
  }
  
  function imageUploadError(data) {
    alert("FAIL!");
  }
  </code></pre>
  
  <p>Even if I respond with an error, the success callback (imageUploadSuccess) is always executed.</p>
  
  <h2>Here's my controller:</h2>
  
  <pre><code>def create
    @image = Image.new params[:file]
    @image.imageable_type = params[:imageable_type]
    @image.imageable_id = params[:imageable_id]
  
    respond_to do |f|
      if @image.save
        logger.debug "PASSED"
        f.text {render :text =&gt; @image.to_json}
      else
        logger.debug "FAIL"
        f.text { render :text =&gt; "Fail!", :status =&gt; 500 }
      end
    end
  end
  </code></pre>
  
  <p>Now, while I could return a json object with <code>success: false</code> in it when it fails, it just feels dirty that the success callback is always executed.</p>
  
  <p><strong>How do I make use of the error callback?</strong></p>