【问题标题】:Rails CanCan - wrong number of arguments (0 for 2+)Rails CanCan - 参数数量错误(0 表示 2+)
【发布时间】:2014-04-21 20:07:31
【问题描述】:

我有这个代码:

  def edit
    @ship = Ship.find(params[:id])
    authorize! if can? :create, @ship
    #authorize! if can? :update, @ship
    #unauthorized! if cannot? :update, @ship
  end

我收到了这个错误:

ArgumentError at /ships/4/edit
wrong number of arguments (0 for 2+)

突出显示这一行:

authorize! if can? :create, @ship

我已经尝试了很多东西,但也尝试了我们之前所拥有的,只是:

authorize! @ship

并且没有用角色重写用户系统,我不确定如何解决这个问题。

这是我的能力等级:

class Ability
  include CanCan::Ability

  def initialize(user)
    #if !user
    if user.try(:admin?)
      can :manage, :all
      return
    elsif !user
      can :read, Ship
      can :read, Planet
      return
    else
      can :read, :all
      #can :manage, Ship, :user_id => user.id
      can :manage, Ship do |ship|
        ship.try(:user) == user
      end
    end
end

【问题讨论】:

  • 有一个新的宝石 CanCanCan 来代替 CanCan - 因为 CanCan 不再维护。即使我能够做到,切换也很容易。

标签: ruby-on-rails ruby devise cancan ruby-on-rails-4.1


【解决方案1】:

authorize! 方法至少需要两个参数 - actionsubject,因此您的代码应该如下所示:

authorize! :create, @ship if can? :create, @ship

【讨论】:

  • 谢谢,我设法通过以下方式修复它:授权! :更新,@ship
猜你喜欢
  • 2015-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多