Exception: CanCan::AccessDenied
- Inherits:
-
Error
- Object
- StandardError
- Error
- CanCan::AccessDenied
- Defined in:
- lib/cancan/exceptions.rb
Overview
This error is raised when a user isn't allowed to access a given controller action. This usually happens within a call to 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. # => "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 error message"
exception. # => "Default error message"
See ControllerAdditions#authorized! for more information on rescuing from this exception and customizing the message using I18n.
Instance Attribute Summary (collapse)
-
- (Object) action
readonly
Returns the value of attribute action.
-
- (Object) default_message
writeonly
Sets the attribute default_message.
-
- (Object) subject
readonly
Returns the value of attribute subject.
Instance Method Summary (collapse)
-
- (AccessDenied) initialize(message = nil, action = nil, subject = nil)
constructor
A new instance of AccessDenied.
- - (Object) to_s
Constructor Details
- (AccessDenied) initialize(message = nil, action = nil, subject = nil)
A new instance of AccessDenied
39 40 41 42 43 44 |
# File 'lib/cancan/exceptions.rb', line 39 def initialize( = nil, action = nil, subject = nil) @message = @action = action @subject = subject @default_message = I18n.t(:unauthorized.default", :default => "You are not authorized to access this page.") end |
Instance Attribute Details
- (Object) action (readonly)
Returns the value of attribute action
36 37 38 |
# File 'lib/cancan/exceptions.rb', line 36 def action @action end |
- (Object) default_message=(value) (writeonly)
Sets the attribute default_message
37 38 39 |
# File 'lib/cancan/exceptions.rb', line 37 def (value) @default_message = value end |
- (Object) subject (readonly)
Returns the value of attribute subject
36 37 38 |
# File 'lib/cancan/exceptions.rb', line 36 def subject @subject end |
Instance Method Details
- (Object) to_s
46 47 48 |
# File 'lib/cancan/exceptions.rb', line 46 def to_s @message || @default_message end |