Question

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
Was it helpful?

Solution

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

authorize! :create, @ship if can? :create, @ship
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top