How to add custom error pages in rails | Ram Laxman Yadav

How to add custom error pages in rails

How ever in practice we often have add custom error pages in rails application. Suppose we need to do some manipulation in the error pages like serve error pages in multiple languages, In this case we can use custom error pages.

Creating error controller:

  • The first step to create a errors controller and create actions for error pages. like this:
      def error_404
        render status: :not_found
      def error_422
        render status: :unprocessable_entity
      def error_500
        render status: :internal_server_error
      # We have to render status according to error because if we don't render status then 200 status will be returns that may messup with requirements.

Create routes for error pages:

  • Add routes for error pages like :
    match "/404", :to => 'errors#error_404', :via => :all
    match "/422", :to => 'errors#error_422', :via => :all
    match "/500", :to => 'errors#error_500', :via => :all

You are done with the custom pages. You can deploy and test your applications.

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer