This article is a continuation of the following article Let's make an error screen with Rails
The class method rescue_from is for catching exceptions that occur in an action, so it cannot catch the processing that occurs at the routing stage.
So you need to create a file in the config / initializers directory.
The action to be called is sorted according to the value of request.path_info.
404 => not_found action 422 => unprocessable_entity Other => internal_server_error
After that, action is passed as an argument to ErrorsController and called.
exceptions_app.rb
Rails.application.configure do
config.exceptions_app = ->(env) do
request = ActionDispatch::Request.new(env)
action =
case request.path_info
when "/404"; :not_found
when "/422"; :unprocessable_entity
else; :internal_server_error
end
ErrorsController.action(action).call(env)
end
end
The controller looks like this: The view to be displayed by render is specified for each action.
errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render status: 404
end
def unprocessable_entity
render status: 422
end
def internal_server_error
render status: 500
end
end
So there are also routing errors that cannot be caught by rescue_from I was able to handle the error and display the screen as described above.
That's all for today.
** 80 days to become a full-fledged engineer **
Recommended Posts