为什么我使用 cancan 时得到的参数 1 代表 0 的数量错误?

发布于 2024-11-28 04:42:24 字数 2493 浏览 0 评论 0原文

我的应用程序中基于角色的授权基于 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。上面的会话模型实例不是这里的身份验证的一部分。 (想一想,法庭现在正在开庭……)。这意味着您必须将这一更改告知 cancan。

我已经重置了 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 行的方法?我已经尝试了数十种替代方案并产生了许多其他错误,但一旦我清理它们,我又回到了这个。每一个建议将不胜感激!

Role based authorization in my app is based on cancan (using rvm 1.9.2@rails_3_0_9 and AuthLogic):

In the view I'm testing I get this:

wrong number of arguments (1 for 0) Extracted source (around line #12):

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

I should explain the usual authentication classes for Authlogic, User and User_session models are replaced in this app with Contact and Contact_sessions. The Session model instance above is not part of authentication here. (Think, the court is now in session...). This means you have to tell cancan about this change.

I've reset the default in 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

Roles and rights are defined in my Ability class, here:

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

And for what it's worth I have not added any code to any other controllers at this point (thinking I would decide what to do once I had can? methods where I want them).

Any idea what's wrong here? I'm assuming the wrong number of arguments is to something invoked by the can? method in line 12 of the view? I've tried dozens of alternatives and generated lots of other errors but as soon as I clean them up I'm back to this one. Every suggestion will be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2024-12-05 04:42:24

我发现:Ability#initialize 方法采用一个参数,该参数是当前用户对象,如果没有用户登录,您可以默认它,如下所示(我的 User 类在此应用程序中被名为 Contact 的类替换):

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

另外,我发现我不需要该角色?方法在 ApplicationController 中,因为显式指定权限并随着时间的推移添加权限非常简单和精确,如下所示(在 Skill#initialize 中):

if current_contact.role == 'superadmin'
  可以:管理,:全部
结尾 

如果 current_contact.role == 'admin'
  可以:管理,

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

if current_contact.role == 'camper'
  可以:读取、会话
  可以[:read,:update],注册#:active => true, :user_id =>

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

 # 访客可以环顾四周并注册(但不能管理

注册)
if current_contact.role == '访客'
可以:读取、会话
可以:更新、注册
结束

I figured it out: the Ability#initialize method takes an argument which is the current user object, and you can default this if there's no user logged in, like this (my User class is replaced in this app by one named Contact):

def initialize(current_contact)
current_contact ||= Contact.create(:role => 'visitor') # guest
user (not logged in)

Also, I found I didn't need the role? method in ApplicationController because it is quite straightforward and precise to specify the permissions explicitly and add to them over time, like this (in Ability#initialize):

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

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

[Accommodation,Cabin,Contact,Identifier,Itinerary,Payment,Resident,Session,Sport]
end

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

user.id if it's their own.... add code for this
end

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

registrations)
if current_contact.role == 'visitor'
can :read, Session
can :update, Registration
end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文