Rails CanCan - wrong number of arguments (0 for 2+)

时间:2022-08-03 23:20:16

I've got this code:

我有这个代码:

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

And I am getting this error:

而我收到此错误:

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

Which is highlighting this line:

这突出了这一行:

authorize! if can? :create, @ship

I've tried quite a few things, but also what we had before, which was just:

我已经尝试过很多东西,但也有我们以前的东西,这只是:

authorize! @ship

And short of re-writing the user system with roles I'm not sure how to solve this.

如果没有用角色重写用户系统,我不知道如何解决这个问题。

This is my ability class:

这是我的能力课:

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 个解决方案

#1


0  

The authorize! method requires two parameters at least - action and subject, so your code should probably look like:

授权!方法至少需要两个参数 - action和subject,所以你的代码可能应该是这样的:

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

#1


0  

The authorize! method requires two parameters at least - action and subject, so your code should probably look like:

授权!方法至少需要两个参数 - action和subject,所以你的代码可能应该是这样的:

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