ユーザーの役割に応じて異なる種類のWikiをインデックスビューに表示します。 admin
とstandard
/guestユーザーの方針は、それほどうまくいくはずですが、プレミアムユーザーとコラボレーションについては少し面倒です。私のアプリでは、コラボレーターをプライベートWikis
に追加する機能があります。だからプレミアムユーザは私のプライベートウィキ、公開ウィキ、プライベートウィキを見ることができるはずですが、プライベートウィキはどこにいても私のために現れません。私のポリシーやモデル団体と関係がありますか?私を助けてくださいPundit Policy:Wikisが共同編集者に公開されていません
ウィキ#インデックス
def index
@wikis = Kaminari.paginate_array(policy_scope(Wiki)).page(params[:page]).per(10)
end
ユーザモデル
class User < ActiveRecord::Base
has_many :wikis
has_many :collaborators
belongs_to :collaborators
....
ウィキモデル
class Wiki < ActiveRecord::Base
belongs_to :user
has_many :collaborators
has_many :users, through: :collaborators
....
コラボモデル
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :wiki
end
Wiki_policy
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
wikis = []
if user.role == 'admin'
wikis = scope.all # if the user is an admin, show them all the wikis
elsif user.role == 'premium'
all_wikis = scope.all
all_wikis.each do |wiki|
if wiki.private == false || wiki.owner == user || wiki.collaborators.include?(user)
wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on
end
end
else # this is the lowly standard user
all_wikis = scope.all
wikis = []
all_wikis.each do |wiki|
if wiki.private == false || wiki.collaborators.include?(user)
wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on
end
end
end
wikis # return the wikis array we've built up
end
end
私はコンソール
last = Wiki.last
last.collaborators
に行くとき、私はこれを取得:
=> #<ActiveRecord::Associations::CollectionProxy [#<Collaborator id: 7, user_id: 8, wiki_id: 104, created_at: "2016-04-24 08:07:20", updated_at: "2016-04-24 08:07:20">]>
を助け
希望を説明してきたように、私は協力者がテーブルに参加しWikiCollaboratorとして、それを再生すると、より多くの意味を行い、その後、実装してドロップすると思う です。あなたが標準ユーザーとしてログインしているときに働いているため、実際にあなたが使用している共同編集者にwikiを割り当てていないと思われます。コードは失敗するようには見えません – joewoodward
私は使用したコンソールでそれをテストしました: 'last = Wiki.last、last.collaborators'、 '#]> ' –