【问题标题】:Rails Pundit ActiveAdmin: page isn’t redirecting properlyRails Pundit ActiveAdmin:页面未正确重定向
【发布时间】:2020-05-15 16:26:15
【问题描述】:

我安装了 Activeadmin 和 Pundit gems。

在 application_controller.rb 中添加了“include Pundit”。

定义 package_policy.rb

class PackagePolicy < ApplicationPolicy
  def update?
    user.admin?
  end
end

application_policy.rb:

class ApplicationPolicy
  attr_reader :user, :record

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

  def index?
    false
  end

  def show?
    false
  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

然后我得到了

page isn’t redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete

在我的浏览器中。也许,它是无限循环或类似的东西。

我有一些不同的 package_policy.rb 配置, 但是在添加 application_policy.rb 之后 - 尝试登录 Activeadmin 面板后,结果总是在浏览器中出错。

【问题讨论】:

标签: ruby-on-rails scope activeadmin policy pundit


【解决方案1】:

我允许我的 ApplicationPolicy 中所有方法的所有操作。

在我为我的资源创建了具有所需权限的新策略之后。

在 ApplicationPolicy 中:

...
  def index?
    true
  end

  def show?
    true
  end

  def create?
    true
  end

  def new?
    create?
  end

  def update?
    true
  end

  def edit?
    update?
  end

  def destroy?
    true
  end
...

在任何其他政策中,例如:

...
  def index?
    user.admin?
  end

  def show?
    user.admin?
  end

  def create?
    user.admin?
  end

  def new?
    create?
  end

  def update?
    user.admin?
  end

  def edit?
    update?
  end

  def destroy?
    user.admin?
  end
...

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 2011-08-11
    • 2018-06-08
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多