简体   繁体   English

用户在带有Rails 4的CanCan初始值设定项中为nil

[英]user is nil in CanCan initializer with Rails 4

Infos : 信息:

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

I'm trying to use cancan gem to manage authorizations access my backoffice (activeadmin). 我正在尝试使用cancan gem来管理对我的后台的访问权限(activeadmin)。 I followed here: the tutorial http://www.activeadmin.info/docs/13-authorization-adapter.html#using_the_cancan_adapter 我在这里遵循:教程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. 问题是,即使有用户登录,“用户”变量在cancan的初始化程序中也始终为nil。

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. 我将调试器放在“除非用户返回,否则返回”行之前,并且“用户”变量始终为nil。 In the views, the "current_user" variable is correctly set BTW. 在视图中,正确设置了“ current_user”变量。

Thanks for your help! 谢谢你的帮助!

EDIT 编辑

I also tried with CanCanCan gem but it doesn't solve the problem! 我也尝试过CanCanCan gem,但不能解决问题!

CanCan is dead and barely supported in Rails 4. CanCan已死,Rails 4几乎不支持它。

You can try: 你可以试试:

https://github.com/CanCanCommunity/cancancan https://github.com/CanCanCommunity/cancancan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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