How to add custom error pages in rails

Posted by : on

Category : 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
  end

  def error_422
    render status: :unprocessable_entity
  end

  def error_500
    render status: :internal_server_error
  end
  
  # 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.



About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in