Display error pages for exceptions

suggest change

To 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents