user1479978
user1479978

Reputation: 247

user is nil in CanCan initializer with Rails 4

Infos :

I'm trying to use cancan gem to manage authorizations access my backoffice (activeadmin). I followed here: the tutorial http://www.activeadmin.info/docs/13-authorization-adapter.html#using_the_cancan_adapter

The thing is the "user" variable is always nil in the cancan's initializer, even when there is a user logged in.

Here is my code:

class Ability
  include CanCan::Ability

  def initialize(user)
    return unless user

    if user.has_operator_role? :active_admin
      can :manage, ActiveAdmin::Page, name: 'Dashboard'
  end
end

I put a debugger before the "return unless user" line and the "user" variable is always nil. In the views, the "current_user" variable is correctly set BTW.

Thanks for your help!

EDIT

I also tried with CanCanCan gem but it doesn't solve the problem!

Upvotes: 0

Views: 741

Answers (1)

Arjan
Arjan

Reputation: 6274

CanCan is dead and barely supported in Rails 4.

You can try:

https://github.com/CanCanCommunity/cancancan

Upvotes: 1

Related Questions