undefined method `accessible_by' for User:Class #792

Open
rutte opened this Issue Dec 11, 2012 · 5 comments

Projects

None yet

5 participants

@rutte
rutte commented Dec 11, 2012

I use mongoid 3 and cancan 1.6.7. Everything works great except checking the index actions. The only difference from the "standard" implementation is that roles are stored as an array in the user model.

When I call

@users = User.accessible_by(current_ability)

I get

undefined method `accessible_by' for User:Class.

Sounds simple, but I can't figure it out. What have I forgotten?

@klebervirgilio

@rutte Could you show the method implementation? Maybe, the method has been implemented as instance method and not a class one.

@rutte
rutte commented Dec 11, 2012

Do you mean accessible_by, thats implemented in cancan. cancan/lib/cancan/model_additions.rb.

My index action looks like this:

def index
  authorize! :index, User

  @users = User.accessible_by(current_ability)         

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users }
  end
end
@sbauch
sbauch commented Dec 17, 2012

you could try something like

load_and_authorize_resource :only => [:index]

def index
    @users = User.accessible_by(current_ability)         

     respond_to do |format|
       format.html # index.html.erb
       format.json { render json: @users }
     end
 end

Such an implementation works for me with mongoid with a non-restful custom action and calling accessible_by on the class.

@trusche
trusche commented Dec 21, 2012

@rutte Are you defining abilities with blocks? Then this is expected behaviour - see #276 and https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks.

@xhoy
xhoy commented Jul 1, 2014

Thanks for your submission! The ryanb/cancan repository has been inactive since Sep 06, 2013.
Since only Ryan himself has commit permissions, the CanCan project is on a standstill.

CanCan has many open issues, including missing support for Rails 4. To keep CanCan alive, an active fork exists at cancancommunity/cancancan. The new gem is cancancan. More info is available at #994.

If your pull request or issue is still applicable, it would be really appreciated if you resubmit it to CanCanCan.

We hope to see you on the other side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment