# Nomethoderror when trying to create permissions

**URL:** <https://www.ruby-forum.com/t/nomethoderror-when-trying-to-create-permissions/182018>\
**Category:** Rails\
**Created:** [January 21, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-create-permissions/182018 "2010-01-21T17:19:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-create-permissions/182018/1 "2010-01-21T17:19:02Z")

</div>

Hey all,

I’m getting a nomethoderror:  
The error occurred while evaluating nil.staff\_admin?

It occurs while I’m trying to create role-based permissions for specific  
actions, such as edit. So a staff admin may not be able to edit. Now I  
understand that it’s trying to say that staff admin is undefined, but I  
believe I did define it in code below. Thanks for all suggestions.

Tables:  
Users: role\_id  
Permissions: primary id, key (e.g. users\_create)  
Roles: primary id, key (e.g. staff member)  
Privileges (join table): role\_id, permissions\_id

Models:  
User  
belongs\_to :role  
delegate :permissions, :to =\> :role

def staff\_admin?  
role == Role[:staff\_admin]  
end

Permission  
has\_many :roles, :through =\> :privileges

Role  
has\_many :users  
has\_many :permissions, :through =\> :privileges

Privileges  
belongs\_to :role  
belongs\_to :permission

ApplicationController \< ActionController::Base

before\_filter :authenticate  
before\_filter :staff\_admin\_required, :except =\> [:edit]

def current\_user  
@current\_user ||= (authenticate\_from\_session ||  
authenticate\_from\_basic\_auth) unless @current\_user == false  
end

protected

def authenticate  
redirect\_to new\_session\_path unless authenticated?  
end

def authorized\_as\_staff\_admin?  
current\_user.staff\_admin?  
end

def current\_user=(new\_user)  
session[:user\_id] = new\_user ? new\_user.id : nil  
@current\_user = new\_user || false  
end

```
def authenticate_from_session
  if session[:user_id] and not session_expired?
    self.current_user = User.find_by_id(session[:user_id])
  end
end

def authenticate_from_basic_auth
  authenticate_with_http_basic do |email, password|
    self.current_user = User.authenticate(email, password)
  end
end

def staff_admin_required
  authorized_as_staff_admin? || user_denied
end

def user_denied
    flash[:notice] = 'You do not have permission to view this page.'
end
```

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2010, 6:48pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-create-permissions/182018/2 "2010-01-21T18:48:52Z")

</div>

2010/1/21 John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Hey all,
> 
> I’m getting a nomethoderror:  
> The error occurred while evaluating nil.staff\_admin?
> 
> It occurs while I’m trying to create role-based permissions for specific  
> actions, such as edit. So a staff admin may not be able to edit. Now I  
> understand that it’s trying to say that staff admin is undefined, but I  
> believe I did define it in code below. Thanks for all suggestions.

It is not exactly undefined, but has the value nil.

Have a look at the Rails Guide on debugging then, using ruby-debug,  
break into the code at the appropriate point and inspect the variables  
to find out what is going on. That will generally be much quicker  
than waiting for someone to analyse your code to work out what is  
wrong.

Good luck

Colin

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-create-permissions/182018/3 "2010-01-21T19:41:52Z")

</div>

Colin L. wrote:

> 2010/1/21 John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hey all,
> > 
> > I’m getting a nomethoderror:  
> > The error occurred while evaluating nil.staff\_admin?
> > 
> > It occurs while I’m trying to create role-based permissions for specific  
> > actions, such as edit. So a staff admin may not be able to edit. Now I  
> > understand that it’s trying to say that staff admin is undefined, but I  
> > believe I did define it in code below. Thanks for all suggestions.
> 
> It is not exactly undefined, but has the value nil.
> 
> Have a look at the Rails Guide on debugging then, using ruby-debug,  
> break into the code at the appropriate point and inspect the variables  
> to find out what is going on. That will generally be much quicker  
> than waiting for someone to analyse your code to work out what is  
> wrong.
> 
> Good luck
> 
> Colin

error was fixed with this:  
def authorized\_as\_staff\_admin?  
current\_user && current\_user.staff\_admin?  
end

But still when logged in as staff admin, there are no restrictions on  
the actions, which was the point of the code. They were supposed to view  
all actions except edit.
