Skip to content
Menu
Railshero
  • Blog
  • About Me
  • Contact Me
  • Privacy Policy
Railshero
custom error pages in rails

Adding custom error pages to our rails app

Posted on July 13, 2021July 15, 2021 by Rails Hero

Default Error Pages

Rails already comes with its own error pages which is okay but too simple, but we can add our own custom error pages, just to make it look a lot nicer, here’s a quick and easy way on how you can do that.

Popular Examples

The default error pages that comes with rails are very minimal, just text based error pages with no images or animations, but these days we can see that a lot of websites use very creative error pages with great looking animations, for example you look at github’s error page or marvel’s error page below.

Source: github.com
Source: github.com
Source: marvel.com
Source: marvel.com

Adding Custom Error Pages

I always customize my error pages in all of my rails apps. To add custom error pages, there are a lot of beautiful and modern looking free templates that we can download and customize, some of them even have creative animations, or we can also create our own error pages from scratch.

Points to keep in mind, when creating/customizing the error pages

  • if you are using images in your error pages, it’s better to encode it in base64 and use base64image, you can convert your images to base64 with lots of tools that you can find online, just search for ‘base64 image’ or when using SVGs, embed it directly don’t add links to external files.
  • make sure to add/embed all the styles and scripts in the same html file, don’t use the asset pipeline.
  • or even better host the images, style sheets and javascripts in a separate server or CDN, you can read this post on how to do so.

So now after creating our own custom error pages

we can move those .html files to our rails app, on this directory

YourRailsApp/public/

make sure to add/replace all of those pages

  • 404.html
  • 422.html
  • 500.html

if you did everything correctly, it will now load your custom error pages. Please keep in mind that error pages will load in ‘production’ environment only, by default. You can quickly test your error pages by deploying your app to heroku.

There are additional steps that you can follow, if you want to see or test the error pages in ‘development’ environment, check the documentation on rails guides if you want to do so. I’ll be posting a tutorial sometime later on how to do that.

How to do it, the DRY way

The above method is a quick and easy way to add custom error pages, but not DRY (because if we wanted to make changes to those error pages, we have to make changes in each of those files individually), for a DRY approach we can create an ‘ErrorPages’ controller and then configure our ‘routes.rb’ file to match the /404, /422 and /500 requests. This way we can use our default application layouts and do a DRY approach. I’ll be posting a tutorial on how to do that too, So stay tuned and good luck with building your rails apps.

1 thought on “Adding custom error pages to our rails app”

  1. wallu manjo says:
    July 15, 2021 at 1:10 am

    what about the DRY approach, please post an update soon ?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tags

active record active storage assets assign to many associations attachment attachments authentication authorization avatar bootstrap cdn config database deploy deployments devise DRY environment variables file uploads Gemfile gems has_many helpers heroku index indexing initializer javascript pagination parameters postgres production public routes.rb ruby gem ruby gems search sendgrid server smtp stylesheets variants views voting

Recent Posts

  • Understanding the DRY Principle in Rails
  • Building Multi-tenant Applications with Rails
  • Rails Basics: Templating Engines
  • Deploying With Capistrano
  • Automated Testing in Rails

Archives

  • July 2023
  • June 2023
  • October 2021
  • September 2021
  • August 2021
  • July 2021

Categories

  • Active Record
  • Activity Logging
  • Apps
  • Assets
  • Attachments
  • Audio
  • Authentication
  • Authorization
  • Deployments
  • Error Pages
  • File Uploads
  • General
  • Heroku
  • Heroku
  • Pagination
  • Rails Basics
  • RubyGems
  • Search Engine Optimization
  • Search/Indexing
  • Testing
  • User Interface
  • Video
  • Views & Templating
  • Voting
  • Web Security
©2025 Railshero | Theme: Wordly by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT