Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 5-如何使用Pundit_Ruby On Rails_Ruby_Pundit - Fatal编程技术网

Ruby on rails Rails 5-如何使用Pundit

Ruby on rails Rails 5-如何使用Pundit,ruby-on-rails,ruby,pundit,Ruby On Rails,Ruby,Pundit,我花了两年的时间尝试学习如何在我的rails应用程序中使用pundit,现在我已经休息了很长时间。我回来了,正在努力学习如何使用权威 我制作了一个全新的rails 5应用程序并安装了pundit 我有一个用户资源、一个应用程序策略和一个用户策略。每个都有: 用户控制器: def index # @users = User.all @users = policy_scope(User) end 应用策略 class ApplicationPolicy attr_reade

我花了两年的时间尝试学习如何在我的rails应用程序中使用pundit,现在我已经休息了很长时间。我回来了,正在努力学习如何使用权威

我制作了一个全新的rails 5应用程序并安装了pundit

我有一个用户资源、一个应用程序策略和一个用户策略。每个都有:

用户控制器:

def index
    # @users = User.all
    @users = policy_scope(User)
  end
应用策略

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def index?
    true
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope
    end
  end
end
用户策略

class UserPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope.where(user: user)
    end
  end


end
有人能看出我是怎么一开始就错了吗?我甚至还没有尝试以我想要的方式定义我的范围,但它在这一点上不起作用

class UserPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope.where(user: user)
    end
  end
end
还因为您在控制器中定义了一个作用域

def index
  # @users = User.all
  @users = policy_scope(User)
end
您不需要在视图中定义另一个

 <% policy_scope(@users).each do |user| %>


这是一个或另一个。在视图中,您可以简单地执行users.each do…

可能您的意思是
范围。其中(id:user.try(:id))
scope.where(id: user.try(:id))
def index
  # @users = User.all
  @users = policy_scope(User)
end
 <% policy_scope(@users).each do |user| %>