Error pages are important. Well, so important, there are even books written about it.
For your shiny new Rails app, you most often want to customize the error pages (404 and 500). Rails’ standard pages are not appropriate for most use cases (how could they).
Usually, you want to include your page logo image, some common page headers, and at least one link back into your application. The layout of the error page should be at least similar to the rest of the application. Now there’s a problem: By default, error pages are static html files. This makes totally sense, since in case of serious server problems, your Rails app might not be able to create the error page dynamically anymore. On the other hand, you cannot reference your standard CSS resources in a static html file since you don’t know the Asset Pipeline ids in advance.
When you google this problem, people recommend pretty complicated strategies like:
- define your own exception app
- monkey patch Rails
- use a gem to create pages without Asset Pipeline ids
- just use ERB and hope that your Rails app would always be alive to serve the error page
Instead, I recommend a much simpler solution:
- Add controller actions for 404 and 500
- Use ERB or Haml to style your error pages nicely
- Right after deployment, call your 404 and 500 actions and save them as static HTML files
It would require only a few lines in Capistrano (thanks to my colleague Ali for figuring out the details):
1 2 3 4 5 6 7 8 9 10 11 12 13 14