【问题标题】:ArgumentError in Users::RegistrationsController#new用户中的 ArgumentError::RegistrationsController#new
【发布时间】:2013-03-18 02:45:39
【问题描述】:

我正在尝试让 Cancan 在我的 Rails 应用程序上使用 Devise。我尝试按照此处的说明进行设置:

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

我遇到以下错误时卡住了:

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

app/models/ability.rb:7:in initialize' app/controllers/users/registrations_controller.rb:6:incheck_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

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

【问题讨论】:

    标签: ruby-on-rails devise cancan


    【解决方案1】:

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

    load_and_authorize_resource

    【讨论】:

    • 是的,解决了这个问题。将 load_and_authorize_resource 添加到我的注册控制器。
    猜你喜欢
    • 2013-06-10
    • 2012-09-24
    • 2015-05-21
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多