Rescuing ActiveRecord RecordNotFound with redirect to

suggest change

You can rescue ActiveRecord::RecordNotFound with a redirect instead of the error page. A redirect is a 3xx response. Passing 404 to redirect_to does not make the browser follow it as a normal redirect. Render a template with status: :not_found when you actually want a 404.

class ApplicationController < ActionController::Base

  # your other stuff

  rescue_from ActiveRecord::RecordNotFound do |exception|
    redirect_to root_path, alert: I18n.t("errors.record_not_found")
  end
end

Feedback about page:

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



Table Of Contents