【问题标题】:Why am I getting wrong number of arguments 1 for 0 using cancan?为什么我使用 cancan 得到的参数 1 对 0 的数量错误?
【发布时间】:2011-08-07 05:48:42
【问题描述】:

我的应用中基于角色的授权基于 cancan(使用 rvm 1.9.2@rails_3_0_9 和 AuthLogic):

在我正在测试的视图中,我得到了这个:

错误数量的参数(1 代表 0)提取的源代码(大约第 12 行):

12: %td = link_to 'Edit', edit_session_path(session) if can? :manage, @session

我应该解释一下 Authlogic、User 和 User_session 模型的常用身份验证类在这个应用程序中被替换为 Contact 和 Contact_sessions。上面的 Session 模型实例在此处不是身份验证的一部分。 (想想,法庭现在正在开庭……)。这意味着你必须告诉康康这个变化。

我已经在 ApplicationController 中重置了默认值:

class ApplicationController < ActionController::Base

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details

  helper_method :current_ability   #:current_contact

  def role?(base_role)
    ROLES.index(base_role.to_s) <= ROLES.index(role)
  end


  # = = = = = = = = = = = = logon controls = = = = = = = = = = = = = = = = = = =   
  private

    # Override default assumption by CanCan
    # https://github.com/ryanb/cancan/wiki/changing-defaults
    # in ApplicationController
    def current_ability
      @current_ability ||= Ability.new(current_contact)
    end  

    def require_contact
      unless current_contact
        redirect_to root_url, :notice => "You must be logged in to access this page."
        return false
      end
    end

    def current_contact_session
      return @current_contact_session if defined?(@current_contact_session)
      @current_contact_session = ContactSession.find
    end

    # return user model
    def current_contact
      return @current_contact if defined?(@current_contact)
      @current_contact = current_contact_session && current_contact_session.record
    end  

end

角色和权限在我的能力类中定义,在这里:

class Ability
include CanCan::Ability

  # Role Inheritance
  # https://github.com/ryanb/cancan/wiki/Role-Based-Authorization
  # in Ability#initialize

  def initialize

    if @contact.role? :visitor
      can :read, [Home, Session]
    end

    if @contact.role? :camper
      can :read, [Home, Contact_session, Session]
      can :manage, Registration
    end

    if @contact.role? :admin
      can :manage, [Home, Contact_session, Contact, Session]
    end

    if @contact.role? :superadmin
      can :manage, :all
    end 

  end

end

而且对于它的价值,我目前还没有向任何其他控制器添加任何代码(我想我会决定一旦我可以做什么?我想要它们的方法)。

知道这里有什么问题吗?我假设错误数量的参数是由罐头调用的?视图第 12 行中的方法?我已经尝试了数十种替代方法并产生了许多其他错误,但是一旦我清理它们,我就会回到这个错误。我们将不胜感激每一个建议!

【问题讨论】:

  • 欢迎来到 Stack Overflow!在发布问题(和答案)时,有可能使问题更具可读性。我已经对您的帖子进行了一些格式化,如果您还有其他想要做的事情,您可以通过编辑链接进行。

标签: gem authlogic cancan


【解决方案1】:

我想通了:Ability#initialize 方法接受一个参数,它是当前用户对象,如果没有用户登录,你可以默认这个,就像这样(我的 User 类在这个应用程序中被一个名为 Contact 的对象替换):

def 初始化(current_contact) current_contact ||= Contact.create(:role => 'visitor') # 访客 用户(未登录)

另外,我发现我不需要这个角色? ApplicationController 中的方法,因为显式指定权限并随着时间的推移添加权限非常简单和精确,就像这样(在 Ability#initialize 中):

if current_contact.role == 'superadmin'
  can :manage, :all
end 

if current_contact.role == 'admin'
  can :manage,

[住宿、机舱、联系方式、标识符、行程、支付、居民、会议、运动] 结束

if current_contact.role == 'camper'
  can :read, Session
  can [:read,:update], Registration #:active => true, :user_id =>

user.id 如果是他们自己的....为此添加代码 结束

 # A visitor can look around and register (but not manage

注册) 如果 current_contact.role == '访客' 可以:阅读,会话 可以:更新,注册 结束

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多