Rails 2.3.11 と cancan 1.1 を使用しています。
ユーザーが特定の組織に属する「出版物」の作成/編集と「出版物」の「記事」の作成/編集を許可する新しい役割を追加したいと考えています。前述したように、古い 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
私は次の3つのモデルを持っています:
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
ご提案ありがとうございます。