Topic: RJS response to Ajax form submission not executed on HTTP 400/424

RJS takes care of the server response on the event the server responds with :success.  respond.to :js enabled and all, the resulting JavaScript is executed automatically.

Thing is, when form validation fails and the server responds with a 400 Bad Request or 424 Unprocessable Entity status code for example, the JS is transmitted but not executed by default.

Steve Schwartz claims this wouldn't be the case: http://www.alfajango.com/blog/rails-3-r … th-jquery/

Also, his blog posts resolve ajax:error callbacks in JavaScript on the client side:  "Just pass errors as JSON and take care of rendering in the browser", the posts say in short.

I think this in not necessary and I wonder why no one suggested the obvious so far:

$(document).ready(function() {
  $('form[data-remote="true"]').bind('ajax:error', function(evt, xhr, status){
    eval(xhr.responseText);
  });
});

(from https://gist.github.com/DivineDominion/5269849)

If you "fall back" to markup errors client-side, you're going to repeat yourself:  (1) in JS of CoffeeScript to render errors after Ajax calls, and (2) it the HTML template for the form itself in case JS is disabled.

Since I can't seem to find a similar approach on the web, I wonder if I missed something.  How do you cope with unsuccessful server responses to Ajax requests?


---
Reference:

I tried to put Steve's code example snippets into a complete Rails project to see if it works.  It didn't.  The 424 Unprocessable Entity response is accompanied by valid JS in the response body, but still nothing was executed automatically.  I put my fix inside.

The full project on GitHub: https://github.com/DivineDominion/rails … m_response
My blog post, explaining the problem: http://christiantietze.de/posts/2013/03 … cks-fixed/