以下是条件
我有users
,他属于一个work
,category
也属于一个work
。
现在,我希望该特定工作的用户对属于该工作的类别进行edit
。
如何在ability
模型中指定这一点?
编辑:
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
user.roles.each do |role|
role.name
if role.name == "admin"
can :read, User
can :create, User
can :edit, User
can :destroy, User
end
if role.name == "staff"
can :read, :all
can :create, :all
cannot :edit, Category
can :update, :all
can :destroy, :all
end
if role.name == "others"
can :read, :all
end
end
end
end
发布于 2013-03-29 08:09:06
未经测试,但这应该可以做到:
can :manage, Category, :work_id => user.work_id
参考资料:https://github.com/ryanb/cancan/wiki/defining-abilities
发布于 2013-03-29 08:48:34
在能力类中,您只传递用户。你也需要通过这个类别才能做到,
can :manage, Category if user.work_id == category.work_id
否则您将不得不像这样迭代(我不会推荐它),
can :manage, Category if user.work_id == user.work.categories.first.work_id
此链接显示如何向您的能力类发送多个参数。
https://stackoverflow.com/questions/15699265
复制相似问题