Dynamic Error

How to generate dynamic error page in Rails

How to generate dynamic error page in rails?

Step: #1

Run command for generating errors controller & view:

$ rails generate controller errors not_found internal_server_error unacceptable

Step: #2

Edit controller like below code:

class ErrorsController < ApplicationController
  def not_found
      render(:status => 404)
  end

  def unacceptable
      render(:status => 422)
  end

  def internal_server_error
      render(:status => 500)
  end
end

Step: #3

Configure routes.rb

match "/404", to: "errors#not_found", via: :all
match "/500", to: "errors#internal_server_error", via: :all
match "/422", to: "errors#unacceptable", via: :all

Step: #4

Configure application.rb

config.exceptions_app = self.routes

Step: #5

Remove default template from public folder like 500.html, 404.html, 422.html

Step: #6

Restart your server then

Step: #7

Test localhost:3000/500, localhost:3000/404,  localhost:3000/422

Step: #8 For development

config.consider_all_requests_local = false

Done!

Happy coding!


Posted

in

,

by

Tags:

Comments

Leave a Reply

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