Nomethoderror when trying to create permissions

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

2010/1/21 John M. [email protected]:

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

Colin L. wrote:

2010/1/21 John M. [email protected]:

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.