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
4

1 回答 1

1

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

authorize! :create, @ship if can? :create, @ship
于 2014-04-21T20:12:40.640 回答