简体   繁体   中英

user is nil in CanCan initializer with Rails 4

Infos :

  • Rails 4.0.2
  • CanCan (1.6.10)
  • Devise (3.2.4)
  • ActiveAdmin (1.0.0.pre)

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!

CanCan is dead and barely supported in Rails 4.

You can try:

https://github.com/CanCanCommunity/cancancan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM