我目前在ability.rb中有类似的东西:
if user.role? == "staff"
can :update, Interp do |item|
item.try(:user) == user
end
can :update, Invitation do |item|
item.try(:user) == user
end
end
实际上,每个角色都有很多这样的“更新”语句。下一个版本也会同样有效吗?
if user.role? == "staff"
can :update, [Interp, Invitation] do |item|
item.try(:user) == user
end
end
有没有更短的方法?
更新:此方法绝对有效,并且是执行此操作所需的最少代码。
最佳答案
这应该有效。如果没有,您可以随时这样做:
if user.role? == "staff"
[Interp, Invitation].each do |resource|
can :update, resource do |item|
item.try(:user) == user
end
end
end
但是,我认为 cancan 足够聪明,能够理解您在这种情况下想要做什么。不过从来没有尝试过。
关于ruby - cancan 能力定义语法 - 是否有更短的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7034241/