我正在使用带有cancan 1.1的Rails 2.3.11 我想添加一个新角色,允许用户在属于一个特定组织的那些“出版物”中创建/编辑“出版物”并创建/编辑“文章”。正如我所提到的,由于我正在运行较旧的cancan 1.1,我无法在doc中尝试所有新示例。这是我在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
感谢您提出任何建议。