Skip to content

Instantly share code, notes, and snippets.

@firedev
Created August 8, 2014 11:04
Show Gist options
  • Save firedev/d0362e99925ba2e813d7 to your computer and use it in GitHub Desktop.
Save firedev/d0362e99925ba2e813d7 to your computer and use it in GitHub Desktop.
Redirects visitor up the restful route if record not found
# controllers/concerns/record_not_found.rb
#
# Redirects visitor up the restful route if record not found
#
# /articles/404 -> /articles/ -> /
module RecordNotFound
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::RecordNotFound do |exception|
redirect_to Hash[:action, :index], alert: (admin? ? exception.message : t('errors.record_not_found'))
end
end
def routing_error
redirect_to "/#{params[:unmatched_route].split('/')[0..-2].join '/'}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment