house9

random code and what not

Ajax error handling with ruby on rails and jquery

This sample is just a slight modification of this post ’Handling AJAX errors and displaying friendly error messages to users’. Basically I wanted to maintain the existing rails error handling for non-ajax pages and then have good handling for errors in both development and production mode; i.e. get errors in the browser when in development mode and show a nice message when in production mode.

Modify /app/controllers/application.rb; add the rescue_from macro and the handler_exception method


Then add a global javascript method for presenting the errors; most likely in application.js

Then a sample usage, jquery making an ajax post to a rails controller action

in development mode the error message is returned in an easier to read view in the firebug console and an alert message is presented that has a truncated version of the error

dev mode

in production mode just a friendly error message.

production mode

Resources