Created
April 8, 2014 16:28
-
-
Save owahab/10151929 to your computer and use it in GitHub Desktop.
Show 403 page for CanCan Access Denied
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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