我用cancan gem工作。
如何获得“CANCAN”来管理两个模型的权限?
谢谢
==================================
我可以这样做????
class Ability
include CanCan::Ability
def initialize(user)
# code
end
def initialize(accounts)
#code
end
end
答案 0 :(得分:4)
class ApplicationHelper
def current_auth_resource
if admin_signed_in?
current_admin
else
current_user
end
end
def current_ability
@current_ability or @current_ability = Ability.new(current_auth_resource)
end
end
class ApplicationController
include ApplicationHelper
end
class Ability
include CanCan::Ability
def initialize(user)
if user.is_a?(Admin)
# add admin permissions only
elsif user.is_a?(User)
# add user permissions only
else
# add guest permissions only
end
end
end
检查this
答案 1 :(得分:2)
您需要在ability.rb文件中指定某些用户可以操作和执行操作的模型。
class Ability
include CanCan::Ability
# usual setup
def initialize(user)
user ||= User.new
if user.has_role? :admin
can :manage, :all
end
if user.has_role? :less_important_role
can [:read, :update], Model1
can :manage, Model2
end
end
end
查看docs以了解有关定义能力的更多信息。