Skip to content

Instantly share code, notes, and snippets.

@owahab
Created April 8, 2014 16:28
Show Gist options
  • Save owahab/10151929 to your computer and use it in GitHub Desktop.
Save owahab/10151929 to your computer and use it in GitHub Desktop.
Show 403 page for CanCan Access Denied
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
rescue_from CanCan::AccessDenied do |exception|
# Make sure we don't redirect to an HTML page
# in API requests because this is absolutely a bad idea
unless request.accept == 'application/json'
# Solutions in order of preference:
# # Solution #1: Show a full page with Access Denied
# render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
# # Solution #2: Redirect to home page and show a flash message
# flash[:error] = "Access denied."
# redirect_to root_url
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment