1

我试图让 Cancan 在我的 Rails 应用程序上与 Devise 一起工作。我尝试按照此处的说明进行设置:

http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

当我收到以下错误时,我被卡住了:

用户中的 ArgumentError::RegistrationsController#new 错误的参数数量(2 比 1)

app/models/ability.rb:7:in initialize' app/controllers/users/registrations_controller.rb:6:incheck_permissions'

我的ability.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

这只发生在注册页面上。应用程序中的所有其他页面都运行良好。

4

1 回答 1

0

使用 cancan 时,您必须在控制器中使用以下行。

load_and_authorize_resource

于 2013-03-18T03:58:49.267 回答