如何让 Active Admin 在登录后与 Pundit 合作

2023-11-26

我已将配置专家适配器授权添加到我的应用程序中

config.authorization_adapter = ActiveAdmin::PunditAdapter

当我登录时[电子邮件受保护]凭据我收到此错误。

Pundit::NotDefinedError in Admin::Dashboard#index
unable to find policy AdminUserPolicy

Extracted source (around line #2):

insert_tag active_admin_application.view_factory["page"]

所以我在策略/active_admin文件夹中创建了这些文件

管理员用户_策略.rb

module ActiveAdmin
class AdminUserPolicy < ApplicationPolicy
class Scope < Struct.new(:user, :scope)
  def resolve
    scope
  end
end
def home?
true
end

def index?
true 
end
def show?
true 
end
def new?
true
end

def create?
 true
end

def update?
true 
end

  def destroy?
    true 
 end
end

end

页面策略.rb

module ActiveAdmin
class PagePolicy < ApplicationPolicy
  class Scope < Struct.new(:user, :scope)
  def resolve
    scope
  end
 end
   def index?
      true
   end

   def show?
     true
   end
  end
end

我缺少什么?谢谢您的帮助!


我找到了答案!

将这两行添加到活动管理初始化程序文件后

config.authorization_adapter = ActiveAdmin::PunditAdapter 

#this line sets the default policy to application_policy.rb
config.pundit_default_policy = "ApplicationPolicy"

我必须将其添加到app/admin/dashboard.rb下的dashboard.rb中

def index
  authorize :dashboards, :index?
end

然后我在策略文件夹中创建了一个名为仪表板策略.rb 的文件并添加了以下代码

class DashboardPolicy < ApplicationPolicy
   def dashboard?
   true
  end
  def index?
   true
  end
 end

这样就可以了!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让 Active Admin 在登录后与 Pundit 合作 的相关文章

随机推荐