Skip to content
wvanbergen edited this page Sep 13, 2010 · 5 revisions

This plugin consists of two parts, the HTTPStatus exceptions and the default exception handler. Both of these can be changed easily to fit your own needs.

Changing the exception handler

This plugin uses rescue_from to catch all HTTPStatus exceptions and simply calls the http_status_exception method to handle it. You can reimplement this method in your application controller if you wish to customize the handling of these exceptions.

This is the default implementation of this method:


def http_status_exception(exception)
@exception = exception
render(:template => exception.template, :status => exception.status)
rescue ActionView::MissingTemplate
head(exception.status)
end

Altering HTTPStatus::Base

All exception classes are automatically generated and based on the HTTPStatus::Base. You can reopen this class to change its functionality, which in turn will then be available to all exception classes. This can be done by adding a file to your config/initializations directory:

# Change the path from  which the error page templates are loaded. 
# Default value: 'shared/http_status'

HTTPStatus::Base.template_path = 'layouts/http_status'

# Add a method to all exception classes
class HTTPStatus::Base
  def hello_world
    'Hello world!'
  end
end
Clone this wiki locally