【发布时间】: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