为什么我使用 cancan 时得到的参数 1 代表 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现:Ability#initialize 方法采用一个参数,该参数是当前用户对象,如果没有用户登录,您可以默认它,如下所示(我的 User 类在此应用程序中被名为 Contact 的类替换):
另外,我发现我不需要该角色?方法在 ApplicationController 中,因为显式指定权限并随着时间的推移添加权限非常简单和精确,如下所示(在 Skill#initialize 中):
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):
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):