Rails CanCan - 错误的参数数量(0 + 2+)

时间:2014-04-21 20:07:31

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

我有这段代码:

  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

1 个答案:

答案 0 :(得分:0)

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

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