Rails3 - CanCan - uninitialized persistent ability :: Page

I just added cancan 1.5.0 to my rails 3 application, here is my features 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 non-calm class, but I am still getting this error -

 uninitialized constant Ability::Page 

Here is the start of 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

+4
source share
2 answers

The CanCan documentation describes the can method as:

The can method is used to determine permissions and requires two arguments. The first is the action for which you set the permission, the second is the class of the object on which you set it.

So the problem is that you do not have a Page class on your system for CanCan for access control.

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

authorization library for Ruby on Rails, which restricts access to resources for this user.

So, if you are focused on managing abstract concepts that don't have relay resources attached to them, you probably won't have a good time with CanCan

+8
source

Just a note for those who find it now ...

You can enable any non-recoverable controller, abstract classes and methods.

Example:

/app/models/role_ability.rb

 class RoleAbility def initialize(user) user ||= User.new if user.role == 'Admin' can :manage, Post # some existing resource_authorisation can :do_this, :on_this # authorizing a non resource end end end 

: do_this and: on_this are completely arbitrary, but they must match authorization! params in the controller, for example ...

 class Controller < ApplicationController def some_abstract_method ### Awesome method code goes here authorize! :do_this, :on_this end end 

Just remember that, most likely, you probably already have permission for the resource happening inside the ApplicationController, maybe this

 class ApplicationController authorize_resource || authorize! :admin, Object || load_and_authorize_resource etc end 

so remember skip_authorize_resource in your immunity / abstract controller

 class AbstractController < ApplicationController skip_authorize_resource def some_abstract_method authorize! :do_this, :on_this end end 

Now the administrator can: do_this ,: on_this and will resolve nicely. You probably want to call this ability more semantically, just want to emphasize randomness.

All this uses Cancan 1.5, have not tried anything before.

From https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers

+6
source

Source: https://habr.com/ru/post/1335270/


All Articles