Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 - CanCan - uninitialized constant Ability::Page

I have just added cancan 1.5.0 to my rails 3 app here is my ability file -

def initialize(user)
 user ||= User.new

if user.role == 'Admin'
  can :manage, :all
end
if user.role == 'Standard'
  can :manage, Library
  can :manage, Page
else
  can :manage, Page
  can :manage, Library
end

I have a custom class (non-restful functions)

class PagesController < ApplicationController
 authorize_resource :class => false

 def home
 end
end

As you can see I am using the correct function for a not restful class but I am still getting this error -

uninitialized constant Ability::Page

Here is the beginning of the stacktrace -

app/models/ability.rb:16:in `initialize'
cancan (1.5.0) lib/cancan/controller_additions.rb:327:in `new'
cancan (1.5.0) lib/cancan/controller_additions.rb:327:in `current_ability'
cancan (1.5.0) lib/cancan/controller_additions.rb:308:in `authorize!'
cancan (1.5.0) lib/cancan/controller_resource.rb:40:in `authorize_resource'
cancan (1.5.0) lib/cancan/controller_resource.rb:9:in `block in add_before_filter'
activesupport (3.0.3) lib/active_support/callbacks.rb:436:in `   _run__1386450187816505438__process_action__15559788756486462__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'

Thanks, Alex

like image 655
Alex Avatar asked Dec 10 '22 11:12

Alex


1 Answers

The CanCan documentation describes the can method as:

The can method is used to define permissions and requires two arguments. The first one is the action you're setting the permission for, the second one is the class of object you're setting it on.

So, the problem is that you don't have a Page class in your system for CanCan to manage access to.

Note that CanCan is built as: (emphasis added by me)

an authorization library for Ruby on Rails which restricts what resources a given user is allowed to access.

So if you are aiming to control abstract concepts which don't have rails resources attached to them then you'll probably have a tough time with CanCan

like image 126
Gareth Avatar answered Dec 28 '22 23:12

Gareth