How to integrate CanCan with multiple devise models?

后端 未结 3 1457
感情败类
感情败类 2021-01-02 01:22

How would I go about defining abilities for several devise models?

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 01:53

    The current user model is passed to the Ability#initialize, so you can just check its class:

    class Ability
      include CanCan::Ability
    
      def initialize(model)
        case model
        when Admin
          can :manage, :all
        when User
          can :create, Comment
          can :read, :all
        else
          can :read, :all
        end
      end
    end
    

提交回复
热议问题