When using cancancan, you might want to show an error message when a user tries to access a page they do not have access to.

In your ApplicationController, add the following:

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to root_url, alert: exception.message
  end