Access CanCan's `can?` method from a model

后端 未结 1 946
礼貌的吻别
礼貌的吻别 2020-12-04 16:48

You can get the current_user\'s permissions from a view or controller using can? in this fashion:

  <% if can? :update, @article         


        
相关标签:
1条回答
  • 2020-12-04 17:11

    There's a wiki entry at github for this: https://github.com/ryanb/cancan/wiki/ability-for-other-users

    You need to change your User model like this:

    class User < ActiveRecord::Base
      def ability
        @ability ||= Ability.new(self)
      end
      delegate :can?, :cannot?, :to => :ability
    end
    

    Then you can check abilities like this:

    user.can?(:update,@article)
    
    0 讨论(0)
提交回复
热议问题