【发布时间】:2017-01-17 23:21:43
【问题描述】:
我正在尝试弄清楚如何将 Pundit 与我的 Rails 5 应用程序一起使用。
我有提案、潜力和用户的模型。这些关联是:
建议
has_many :potentials, inverse_of: :proposal
accepts_nested_attributes_for :potentials, reject_if: :all_blank, allow_destroy: true
belongs_to :user
潜力
belongs_to :proposal, inverse_of: :potentials
belongs_to :user
用户
has_many :proposals, dependent: :destroy
has_many :potentials
我对每种资源都有权威政策。
我目前正在努力弄清楚如何实施规则,以便仅在应用规则时才显示潜力。
我的潜力呈现在我的提案视图文件夹中保存的部分中。它有:
<% @proposal.potentials.each do | pot | %>
<div class="panel">
<% if policy(pot).show? %>
<% if pot.private_comment == true %>
<p> <%= render :text => 'CONFIDENTIAL - NOT FOR PUBLIC DISCLOSURE' %></p>
<% end %>
<p><%= pot.comment %>
</p>
<p style = "color: navy; text-align:right"><%= pot.user.full_name %>, <%= pot.user.organisation.title.titleize %></p>
<p style="font-style:italic; color: #FFFFFF; float:right"><%= text_for_status(pot)%></p>
</div>
<% end %>
<% end %>
在我的提案控制器中,显示操作,我有:
before_action :set_proposal, only: [:show, :edit, :update, :destroy ]
def show
@potentials = @proposal.potentials
end
private
# Use callbacks to share common setup or constraints between actions.
def set_proposal
@proposal = Proposal.find(params[:id])
authorize @proposal
end
在我的潜在政策中,我将展示规则定义为列出的 3 个条件中的任何一个为真:
class PotentialPolicy < ApplicationPolicy
def index?
true
end
def show?
true if record.private_comment != true ||
if record.private_comment == true && @current_user == record.user_id ||
if record.private_comment == true && @current_user == record.proposal.user_id
else false
end
end
结束
def new?
true
end
def create?
true
end
def edit?
update?
end
def update?
true if record.user_id == current_user.id
end
def destroy?
false
end
end
我的期望是,由于我要求在 views/proposals/temporary_proposals.html.erb 部分(上)中检查潜在政策并在下面提取,
<% if policy(pot).show? %>
注意:pot 定义为@proposal.potential。
我能看到的唯一逻辑错误是当前用户是用户而不是用户 ID。但是,如果我将“.id”附加到 current_user 的末尾,我会收到一条错误消息,显示“id is nil”。
Pundit 将查看该提案的潜在政策,并确定是否显示记录。
这是不对的,因为当我保存所有这些并尝试呈现提案节目时,我只能看到 :private_comment 属性不正确的潜力(但我确实符合第二个和第三个合格许可的标准(即我创造了潜力和提案)-所以我应该能够查看该记录)。
我的申请政策有:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
我理解这意味着由于我的潜在策略继承自我的应用程序策略,我应该能够引用 @record 来表示此策略试图处理的记录。对我来说,这意味着我应该能够将 show 动作定义为:
def show?
true if record.private_comment != true ||
if record.private_comment == true && @current_user == @record.user ||
if record.private_comment == true && @current_user == @record.proposal.user
else false
end
end
end
但这给出了与上述尝试相同的错误结果。
谁能看出我哪里做错了?
【问题讨论】:
标签: ruby-on-rails ruby authorization pundit