« back to all cards in this deck
Posted over 1 year ago. Visible to the public. Repeats.

Custom error pages in Rails 3.2

Starting with Rails 3.2, there is a nicer way to display custom error pages (i.e. "page not found") that does not interfere with ExceptionNotifier. (I'm not sure about Airbrake et al.)

José Valim has posted this on his blog, here is a slightly more verbose rundown:

  1. Register your own app as the application responsible for showing error pages by adding

    config.exceptions_app = self.routes

    to your config/application.rb.

  2. Add routes for the error pages, by appending this to your config/routes.rb:

    match '/404' => 'errors#not_found' match '/422' => 'errors#server_error' match '/500' => 'errors#server_error'
  3. Add an ErrorsController like this one:

    class ErrorsController < ApplicationController def not_found render :status => 404, :formats => [:html] end def server_error render :status => 500, :formats => [:html] end end
  4. Add the corresponding views.

  5. Add some tests, for example a cucumber feature:

    @allow-rescue Scenario: 404 page When I go to the path "/foobar" Then the page should be titled "Page not found" And I should see "We're sorry, but couldn't find the page you requested" And the response status should be "404"

Additional notes

  • You cannot modify headers, so you can't modify the session, cookies etc.
  • You won't see this in development, except if you set config.consider_all_requests_local = false in your config/development.rb.

Author of this card:

Avatar
Tobias Kraze
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
Posted by Tobias Kraze to makandropedia