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