cancan 能力定义语法 - 是否有更短的版本?
我目前在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
有更短的路吗?
更新:此方法绝对有效,并且是执行此操作的代码量最少的方法。
I currently have something like this in 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
Actually, there are a bunch of statements like this for "update" for each role. Would this next version work just as well?
if user.role? == "staff"
can :update, [Interp, Invitation] do |item|
item.try(:user) == user
end
end
Is there a shorter way?
UPDATE: This method definitely works and is the least amount of code to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该可以工作。如果没有,你总是可以这样做:
但是,我认为 cancan 足够聪明,能够理解你在这种情况下想要做什么。但从未尝试过。
That should work. If it doesn't, you can always do it like this:
However, I think cancan is smart enough to understand what you're trying to do in this case. Never tried it, though.