Ruby on Rails ActionController Rescuing ActiveRecord::RecordNotFound with redirect_to


Example

You can rescue a RecordNotFound exception with a redirect instead of showing an error page:

class ApplicationController < ActionController::Base

  # your other stuff

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