Rails - Pundit - 解决方法运行不正常
Rails - Pundit - resolve method not operating correctly
我正在尝试弄清楚如何在我的 rails 4 应用程序中使用 pundit。
我有文章政策和文章索引视图。
在文章策略中,我有一个针对不同类型用户的解析方法。
def resolve
if user == @user.id
scope.all.where(user_id: user.id)
elsif user.has_role?(:org_approver)
scope.to_be_reviewed
else
scope.in_state(:publish)
end
end
在我的索引显示中,我希望向每个用户显示数组中的文章,具体取决于他们在我的 resolve 方法中属于哪个类别。
<% policy_scope(Article).sort_by(&:created_at).in_groups_of(2) do |group| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<% group.compact.each do |article| %>
<div class="col-md-4 col-md-offset-1 portfolioitem Scienza">
<div class="portfolio-item text-
我没有收到任何错误,但是当我以文章作者用户身份登录时,我希望获得属于我(作为该用户)的所有文章。相反,我只获取所有已发表的文章(这是我的 resolve 方法中的最后一个选择)。
谁能看出我哪里出错了?
我认为这一行是问题所在:
user == @user.id
应该是:
user == @user
因为 User
对象永远不会等于 Integer
对象,这就是您在 User
对象上调用 id
时得到的结果。
此外,请查看 Pundit GitHub page 并向下滚动到有关测试的 RSpec 部分。 您应该始终测试身份验证和授权。
我正在尝试弄清楚如何在我的 rails 4 应用程序中使用 pundit。
我有文章政策和文章索引视图。
在文章策略中,我有一个针对不同类型用户的解析方法。
def resolve
if user == @user.id
scope.all.where(user_id: user.id)
elsif user.has_role?(:org_approver)
scope.to_be_reviewed
else
scope.in_state(:publish)
end
end
在我的索引显示中,我希望向每个用户显示数组中的文章,具体取决于他们在我的 resolve 方法中属于哪个类别。
<% policy_scope(Article).sort_by(&:created_at).in_groups_of(2) do |group| %>
<div id="portfolioFiltering" class="masonry-wrapper row">
<% group.compact.each do |article| %>
<div class="col-md-4 col-md-offset-1 portfolioitem Scienza">
<div class="portfolio-item text-
我没有收到任何错误,但是当我以文章作者用户身份登录时,我希望获得属于我(作为该用户)的所有文章。相反,我只获取所有已发表的文章(这是我的 resolve 方法中的最后一个选择)。
谁能看出我哪里出错了?
我认为这一行是问题所在:
user == @user.id
应该是:
user == @user
因为 User
对象永远不会等于 Integer
对象,这就是您在 User
对象上调用 id
时得到的结果。
此外,请查看 Pundit GitHub page 并向下滚动到有关测试的 RSpec 部分。 您应该始终测试身份验证和授权。