class CanCan::AccessDenied

This error is raised when a user isn't allowed to access a given controller action. This usually happens within a call to CanCan::ControllerAdditions#authorize! but can be raised manually.

raise CanCan::AccessDenied.new("Not authorized!", :read, Article)

The passed message, action, and subject are optional and can later be retrieved when rescuing from the exception.

exception.message # => "Not authorized!"
exception.action # => :read
exception.subject # => Article

If the message is not specified (or is nil) it will default to “You are not authorized to access this page.” This default can be overridden by setting default_message.

exception.default_message = "Default error message"
exception.message # => "Default error message"

See ControllerAdditions#authorized! for more information on rescuing from this exception.

Attributes

action[R]
default_message[W]
subject[R]

Public Class Methods

new(message = nil, action = nil, subject = nil) click to toggle source
# File lib/cancan/exceptions.rb, line 32
def initialize(message = nil, action = nil, subject = nil)
  @message = message
  @action = action
  @subject = subject
  @default_message = "You are not authorized to access this page."
end

Public Instance Methods

to_s() click to toggle source
# File lib/cancan/exceptions.rb, line 39
def to_s
  @message || @default_message
end