【发布时间】:2014-05-23 05:00:14
【问题描述】:
我想在能力文件中为多态关联指定规则如下:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :admin
can :manage, :all
elsif user.role? :author
can :read, :all
can :manage, [Post, Article], user_id: user.id
can :manage, Comment, commentable: {user_id: user.id}
end
end
end
我希望用户能够编辑对其帖子的评论。但是在检查访问时,简单用户仍然可以编辑其他(所有)cmets。
这个技能对康康有效吗?
can :manage, Comment, commentable: {user_id: user.id}
感谢您的帮助!
======= 更新 =======
对不起,我忘了展示我的能力检查:
- can? :manage, @post => Comment
并试图重写:
= can? :manage, @post.comments.build
但我不喜欢这种检查,因为创建了空对象
【问题讨论】:
标签: ruby-on-rails ruby polymorphism cancan polymorphic-associations