What is current_ability in CanCan's accessible_by (fetching records)?
Asked Answered
L

1

13

In the documentation of CanCan it shows how to fetch all accessible records in this way:

@articles = Article.accessible_by(current_ability)

but what is current_ability? I've tried passing the current user which I'm using for authentication and authorization, but I've got this error:

NoMethodError: undefined method `conditions' for #<User:0x1092a3b90>

Any ideas what should I pass to accessible_by or what's wrong here?

Losse answered 30/7, 2010 at 5:34 Comment(0)
T
12

CanCan extends ActionController::Base with the current_ability method (among others), which is the class that your ApplicationController should inherit from. So the example code in your question should work as is.

Tanguy answered 30/7, 2010 at 6:54 Comment(2)
You can also pass in your own ability using an instance of the ability class you've defined. The ability class is the one that include's CanCan::Ability, and you can use it like so: Ability.new(current_user)Waspish
i guess many people asked themselves this question. maybe the wiki documentation should be updated?Jone

© 2022 - 2024 — McMap. All rights reserved.