Rail Validation Error Handling – Education Career Blog

Currently when my rails app encounters an error it dumps the error code and stack trace onto the browser window.

How do I turn this off so that these error are only recorded in the log and not outputted to the browser?

Thank you

Correction: I am mostly concerned with only the validation errors. All I want is the same functionality you get when using generated scaffolding. I would like to show the validation error messages and highlight the offending form fields. Right now all I get is this standard development error output. Originally this was working on my app but somehow it was lost.

,

When your server encounters an error in production mode, it will show a default rails error page and not the stack trace that you see in development mode.

You can add custom static html 500 or 404 error pages by putting them in public/500.html and public/404.html.

If you would like to handle errors differently, you can put the following code in your ApplicationController (This code is untested, but is based on a very similar method in one of my projects):

around_filter :handle_errors

def handle_errors
  begin
    yield
   rescue Exception => e
     # Handle the exception however you wish.
   end
end

Note that the code above will not handle 404 errors. However, you can specify a default action to handle routes that can not be located by putting the following at the bottom of config/routes.rb:

map.connect '*path', :controller => 'home', :action => 'page_not_found'

Edit:

After doing a bit more research, there is an alternate and likely better way of custom error handling: http://brian.pontarelli.com/2007/01/14/handling-rails-404-and-500-errors/. Essentially the recommendation is to overwrite the rescue_action_in_public method like below:

  def rescue_action_in_public(exception)
    render :template => "shared/error", :layout => "standard", :status => "500"
  end

Leave a Comment