我想学习如何使用我的Rails 5应用程序的专家。Rails 5 - Pundit Scopes
我正试图按照这篇文章中的方法。
https://learn.co/lessons/devise_pundit_readme
我很努力地从这种方法中找到工作结果。具体而言,在我的索引行,我尝试使用定义的范围,是:
<% policy_scope(@user.proposals).each do |prop| %>
将返回该错误:
undefined method `proposals' for nil:NilClass
我的设置是:
我对用户模型和建议。该协会是:
用户:
has_many :proposals
提案:
belongs_to :user
应用控制器:
include Pundit
提案控制器:
def index
@proposals = Proposal.all
# @bips = @proposal.bips
authorize @proposals
end
项
建议政策:
class ProposalPolicy < ApplicationPolicy
class Scope < Scope
def resolve
if user.has_role?(:admin)
scope.all
else
scope.where(:current_state == :draft)
end
end
end
def index?
true
end
建议指标:
<% policy_scope(@user.proposals).each do |prop| %>
<%= prop.title %>
<%= prop.user.full_name %>
<%= prop.created_at %>
<%= link_to 'More', proposal_path(prop) %>
<% end %>
当我尝试,我得到一个错误 - 高亮这一行我的索引的问题:
<% policy_scope(@user.proposals).each do |prop| %>
鉴于这与我链接的文章完全相同,因此我失去了为什么它不起作用。
错误说:
undefined method `proposals' for nil:NilClass
我链接的文章和权威人士文件之间的主要区别是,权威人士自述说:
关于类范围 - 自述只是有:
类范围
即它没有额外的“<范围”
自述中邮政策有这个(我有它在我的应用程序策略 - 但试图将它添加到建议政策以及):
attr_reader:用户:范围
自述中邮政策有这个(我有它在我的应用程序策略 - 但试图将它添加到建议政策以及): (用户,范围) @用户=用户 @Scope =范围 结束
我试图做它的权威人士文档自述显示的方式 - 但我得到了同样的错误,当我尝试文章如下。
我试图改变的决心方法,以便它总是返回scope.all:
def resolve
if user.has_role?(:admin)
scope.all
else
scope.all
end
但我仍然得到同样的错误。
任何人都可以看到我需要做什么才能使用Pundit范围?今年早些时候
回读通过我相关的问题 - 我可以看到,无论是在创业板权威人士自述也不教程文章我联系似乎是正确的:Rails 4 - Pundit - scoped policy for index
我不能得到任何的解决方案从链接的SO帖子开始工作 - 但这些答案中的概念比宝石自述文件和教程更彻底。
下一次尝试
东西是腥与权威人士的方式找到用户。
我将解析方法更改为非常简单的事情 - 检查用户的名字值是否等于一个字符串(这是我用户名的第一个用户名的记录) :
class ProposalPolicy < ApplicationPolicy
class Scope #< Scope
# class Scope
attr_reader :user, :scope
def initialize(user, scope)
user = user
scope = scope
end
def resolve
if user.first_name == "Jane"
scope.all
else
scope.where(:current_state == :draft)
end
end
end
当我尝试,我得到一个错误,指出:
undefined method `first_name' for nil:NilClass
我不认为权威人士知道如何找到用户。
当我把一个byebug在我的建议索引操作,我得到:
user.inspect
*** NameError Exception: undefined local variable or method `user' for #<ProposalsController:0x007fa3e8cf8698>
(byebug) @user
nil
(byebug) current_user
#<User id: 43, first_name: "Jane",
这样的 - 我是错的,我多么想有权威人士识别用户。我已经梳理了GitHub上的数百个回购版,试图找到人们如何获得这个工作的例子。我无法找到与我在链接帖子中尝试的所有选项不同的任何内容。
我不知道,如果是陌生的,但我可以这样做:
(byebug) policy_scope(Proposal)
Proposal Load (0.4ms) SELECT "proposals".* FROM "proposals"
#<ActiveRecord::Relation [#<Proposal id: 17, user_id: 43, title: "asdf", description: "adsf", byline: "asdf", nda_required: true, created_at: "2016-11-16 00:28:31", updated_at: "2016-11-28 01:16:47", trl_id: 1>]>
user_ID的43拥有first_name的属性,它是简。我想这可能是一个迹象,表明事情是工作,但是当我改变决心的方法来索要“约翰”,而不是“简”,我得到了相同的结果:
(byebug) policy_scope(Proposal)
Proposal Load (0.8ms) SELECT "proposals".* FROM "proposals"
#<ActiveRecord::Relation [#<Proposal id: 17, user_id: 43, title: "asdf", description: "adsf", byline: "asdf", nda_required: true, created_at: "2016-11-16 00:28:31", updated_at: "2016-11-28 01:16:47", trl_id: 1>]>
显然,这一次,它是不正确,因为此提案所属用户的名字不是John。
有人能看到什么,我需要做的就是权威人士识别用户?
'nil:NilClass'的未定义方法'提案'表示用户为'nil',您能否提升user.inspect'并显示输出 –
在'policy'的'index'上,您将如何获得'@user '直到你在控制器中定义它?无论是有定义,或者如果您使用的任何认证宝石样色器件然后尝试'current_user',而不是'@ user'。 – Deep
我不确定你的意思。用户是在应用程序策略中传递给作用域方法的两个属性之一。 – Mel