Rescuing ActiveRecord RecordNotFound with redirect to
suggest changeYou 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents