我想让Cancan在我的Rails应用程序上使用Devise。我试着按照这里的说明进行设置:
我遇到了以下错误:
用户中的ArgumentError :: RegistrationsController #new 错误的参数数量(2对1)
app / models / ability.rb:7:initialize'
app/controllers/users/registrations_controller.rb:6:in
check_permissions'
我的能力.rb内容如下:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :super_admin
can :manage, :all
elsif user.role? :admin
can :manage, :all
elsif user.role? :user
can :read, :all
# manage products, assets he owns
can :manage, Product do |product|
product.try(:owner) == user
end
can :manage, Asset do |asset|
asset.assetable.try(:owner) == user
end
end
end
end
我的注册控制器上写着:
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :check_permissions, :only => [:new, :create, :cancel]
skip_before_filter :require_no_authentication
def check_permissions
authorize! :create, resource
end
end
这仅发生在注册页面上。应用程序中的所有其他页面都正常工作。
答案 0 :(得分:0)
使用cancan时,您必须在控制器中使用以下行。
load_and_authorize_resource