Display error pages for exceptions
suggest changeTo show a specific error instead of the generic “something went wrong” page, rescue that exception in the controller.
Open the file app/controllers/application_controller.rb and you should find something like this:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
We can now add a rescue_from to recover from specific errors:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private
def record_not_found
render html: "Record <strong>not found</strong>".html_safe, status: :not_found
end
end
Do not rescue Exception or StandardError. That hides unexpected failures and stops Rails from showing its error pages. This handler also does not run for exceptions raised outside a controller, such as in a middleware.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents