我有一个Post模型,其中有一个:published
属性(布尔类型),以及一个User模型,其中有一个role
属性(字符串)。有三种角色:ROLES = %w [admin publisher author]
我不希望角色为author的用户能够在Post模型上设置或编辑:published
字段。
我正在使用CanCan(和RailsAdmin宝石),我的简化Ability.rb文件如下:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role? :admin
can :manage, :all
elsif user.role? :publisher
can :manage, Post
elsif user.role? :author
# I want to prevent these guys from setting the :published attribute
end
end
end
有人对这种事情有什么建议吗?
cannot <<your attribute>>, :klass bee
,因为它们在等待2.0时仍然是Ruby中的方法。 - Francesco Belladonna