我正在使用带有 cancan 1.1 的 Rails 2.3.11。
我想添加一个新角色,允许用户在属于某个特定组织的那些“出版物”中创建/编辑“出版物”并创建/编辑“文章”。正如我所提到的,由于我运行的是较旧的 cancan 1.1,我无法尝试文档中的所有新示例。这是我在 Ability.rb 中尝试的一个示例:
class Ability
include CanCan::Ability
#additional roles require changes be made to config/environment.rb ROLES constant
def initialize(user)
user ||= User.new # Guest user
# Multirole environment
if user.role? :admin
can :manage, :all
can :archive, Article
can :review, Article
end
#several other roles ...
#This is not working...I'm specify the organization that I want the super_user to be able to create and edit for
if user.role? :super_user
can [:create, :edit], Publication, :organization_id => 21
can [:create, :edit], Article, :organization_id => 21
end
我有以下三个模型:
class Organization < ActiveRecord::Base
#validations here
has_many :publications, :dependent => :destroy
has_many :articles, :through => :publications
end
class Publication < ActiveRecord::Base
#validations here
has_many :articles, :dependent => :destroy
end
class Article < ActiveRecord::Base
#validations here ...
belongs_to :publication
validates_associated :publication
#...
end
感谢您的任何建议。