【问题标题】:How to pass single method with multiple previleges to 'can?' method of cancan?如何将具有多种权限的单一方法传递给“可以”?康康的方法?
【发布时间】:2013-07-21 02:38:28
【问题描述】:

我正在使用 devise 和 cancan gem 来管理我的 rails 应用程序中的身份验证和权限。该应用程序是关于图书馆管理的。我在ability.rb 中定义了方法,例如:

def lend
  can :manage, Book
  can :manage, Transactions
end

然后在ability.rb中,我将权限分配为

if user.role? == "librarian" 
  can lend
end

在视图中,对于具有“图书管理员”角色的用户,我可以使用 can?(:manage, Book)can?(manage, Transaction)

但是,我希望能够使用current_user.can?(:lend)。我跟着 this github page做可以吗?方法在用户模型中可用。这让我可以使用

current_user.can?(:manage, Book)

但是,current_user.can?(:lend) 结果为wrong number of arguments(1 for 2+)。我有一种感觉,我在这里缺少一点智慧。我在这里尝试了谷歌和相关帖子,但无济于事。请帮忙。

【问题讨论】:

    标签: ruby-on-rails devise cancan


    【解决方案1】:

    can? 有两个必需的参数:

    def can?(action, subject, *extra_args)
    

    因此:wrong number of arguments(1 for 2+)

    cancan/lib/cancan/ability.rb:56

    此外,在您的 ability.rb(下)中,既没有 action 也没有名为 :lendsubject。方法名与cancan无关。

    def lend
      can :manage, Book
      can :manage, Transactions
    end
    
    if user.role? == "librarian" 
      can lend
    end
    

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 2021-01-31
      相关资源
      最近更新 更多