2
我试图阻止各种项目的编辑能够使用cancan发布自己的作品,但它不能按预期工作。迄今为止的其他一切都很完美。rails cancan自定义动作
例如:
can :publish, [Article, Review] do |doc|
doc.user != @user
end
查看
<% if can? :publish, @review %>
我遵循设置自定义操作的文档,但到目前为止,我还没有成功。
https://github.com/ryanb/cancan/wiki/Custom-Actions
ability.rb
class Ability
include CanCan::Ability
def initialize(user)
@user = user || User.new # for guest
@user.roles.each { |role| send(role) }
if @user.roles.size == 0
can :read, :all #for guest without roles
end
end
def author
can :manage, [Article, Review] do |doc|
doc.try(:user) == @user
end
can :submit, [Article, Review]
end
def editor
can :manage, [Article, Review]
can :publish, [Article, Review] do |doc|
doc.user != @user
end
end
def admin
can :manage, :all
can [:submit, :publish, :reject], [Article, Review]
end
end