【发布时间】: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!在发布问题(和答案)时,有可能使问题更具可读性。我已经对您的帖子进行了一些格式化,如果您还有其他想要做的事情,您可以通过编辑链接进行。