【问题标题】:Pundit Policy : Wikis not showing up for collaborators权威政策:未向合作者显示 Wiki
【发布时间】:2023-03-21 00:35:01
【问题描述】:

我希望根据用户的角色在索引视图中显示不同类型的 wiki。 adminstandard / 来宾用户的政策可以正常工作,但对于高级用户和协作,它会变得有点混乱。在我的应用程序中,我可以将协作者添加到私人 Wikis 。因此,高级用户我应该能够看到我正在合作的我的私人 wiki、公共 wiki 和私人 wiki,但我的合作者所在的私人 wiki 并没有显示给我。这可能与我的政策或我的模特协会有关吗?请帮帮我

wiki#index

  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

维基政策

   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">]>

【问题讨论】:

  • 尝试在控制台中手动测试代码。我怀疑您实际上并未将 wiki 分配给您正在使用的协作者,因为当您以标准用户身份登录时它正在工作。代码看起来不应该失败
  • 我在我使用的控制台上测试了它:last = Wiki.last , last.collaborators 并得到了#&lt;ActiveRecord::Associations::CollectionProxy [#&lt;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"&gt;]&gt;

标签: ruby-on-rails policy pundit bloc.io


【解决方案1】:

啊,我明白了。您的连接表格式不正确,这可能是因为您对同一类同时使用了 belongs_to 和 has_many,所以您对命名感到困惑。即 belongs_to :user 和 has_many :users

你需要利用 through 定义中的 class_name

Wiki
  # the owner
  belongs_to :user

  # the collaborators join table
  has_many :wiki_collaborators

  # this is the object you will call from wiki.collaborators
  has_many :collaborators, through: :wiki_collaborators, class_name: 'User'
end

User
  # the wikis owned by this user
  has_many :wikis

  # the join table
  has_many :wiki_collaborators

  # this is the object you can call from user.wiki_collaborations or something else that maybe fits better
  has_many :wiki_collaborations, through: :wiki_collaborators, class_name: 'Wiki'
end

WikiCollaborator
  belongs_to :user
  belongs_to :wiki
end

您的问题是,当您调用 wiki.collaborator 时,您实际上是在返回连接模型而不是用户模型。

您实际上可以调用 wiki.users(注意复数)来获取您当前拥有的代码中的 wiki 合作者。 不过,您仍然需要修复 User belongs_to :collaborators 行。 我认为删除 Collaborator 连接表并将其重新生成为 WikiCollaborator 会更有意义,然后按照我的描述实现

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多