登录后如何让 Active Admin 与 Pundit 一起工作 [英] How to get Active Admin to work with Pundit after login
问题描述
我已将配置 Pundit addapter 授权添加到我的应用程序中
I've added the configuration pundit addapter authorization to my application
config.authorization_adapter = ActiveAdmin::PunditAdapter
当我使用 admin@example.com 凭据登录时,出现此错误.
When I login with the admin@example.com credentials I'm getting this error.
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 文件夹中创建了这些文件
so I created these files in my policies/active_admin folder
adminuser_policy.rb
adminuser_policy.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
结束
page_policy.rb
page_policy.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
我错过了什么?感谢您的帮助!
What am I missing? Thanks for the help!
推荐答案
我找到了答案!
将这两行添加到活动的管理初始化文件后
After adding these two lines to the active admin initializer file
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
I had to add this to dashboard.rb under app/admin/dashboard.rb
def index
authorize :dashboards, :index?
end
然后我在我的策略文件夹中创建了一个名为dashboard_policy.rb 的文件并添加了这个代码
Then I created a file in my policies folder called dashboard_policy.rb and added this code
class DashboardPolicy < ApplicationPolicy
def dashboard?
true
end
def index?
true
end
end
它开始工作了!
这篇关于登录后如何让 Active Admin 与 Pundit 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!