我试图阻止各种项目的编辑能够使用 cancan 发布他们自己的作品,但它没有按预期工作。到目前为止,其他一切都运行良好。
例如:
can :publish, [Article, Review] do |doc|
doc.user != @user
end
看法
<% if can? :publish, @review %>
我按照文档设置了自定义操作,但到目前为止我还没有取得任何成功。
https://github.com/ryanb/cancan/wiki/Custom-Actions
能力.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