Posted over 7 years ago. Visible to the public.

Render custom 404 page in Rails 2

Simple

Tell the application controller how to handle exceptions, here a RecordNotFound error.
Do this with the following line:

Copy
rescue_from ActiveRecord::RecordNotFound, :with => :render_404

This will call the method render_404 whenever a RecordNotFound error occurs (you could pass a lambda instead of a symbol, too).
Now write this method:

Copy
def render_404 render 'errors/404', :status => '404' end

Finally create a 404 document views/errors/errors.html.haml.

Copy
%h1 Record Not Found .error The record you requested could not be retrieved in my database.

Any 404 error that is caused by something not found in your Rails app will render a nice message using your default layout. Similarly, you may catch and handle any other error.

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
over 6 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more