登录后如何使Active Admin与Pundit一起使用 [英] How to get Active Admin to work with Pundit after login

查看:89
本文介绍了登录后如何使Active Admin与Pundit一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将配置pundit addapter授权添加到应用程序中

  config.authorization_adapter = ActiveAdmin :: PunditAdapter 

当我使用admin@example.com凭据登录时,出现此错误。

  Pundit :: NotDefinedError Admin :: Dashboard#index 
无法找到策略AdminUserPolicy

提取的源(大约第2行):

insert_tag active_admin_application.view_factory [ page]

所以我在我的policy / active_admin文件夹中创建了这些文件



adminuser_policy.rb

  module ActiveAdmin 
class AdminUserPolicy< ApplicationPolicy
class范围< Struct.new(:user,:scope)
def resolve
作用域
end
end
def home?

结束

def指数?
真实的
结束
def显示吗?

结束
def新?

结束

def创建吗?

结束

def更新?

结束

防御破坏了吗?
真实
结束
结束

结束



page_policy.rb

  module ActiveAdmin 
class PagePolicy< ApplicationPolicy
class范围< Struct.new(:user,:scope)
def解析
作用域
end
end
def索引?

结束

def显示吗?
真实
结束
结束
结束

什么我想念吗?谢谢您的帮助!

解决方案

我找到了答案!



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

  config.authorization_adapter = ActiveAdmin :: PunditAdapter 

#此行将默认策略设置为application_policy.rb
config.pundit_default_policy = ApplicationPolicy

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

  def index 
授权:dashboards,:index?
end

然后我在策略文件夹中创建了一个名为dashboard_policy.rb的文件,并添加了该文件代码

 类DashboardPolicy< ApplicationPolicy 
def仪表板?
true
最终
def指数?
真实的
结束
结束

这确实起作用了! / p>

I've added the configuration pundit addapter authorization to my application

config.authorization_adapter = ActiveAdmin::PunditAdapter

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"]

so I created these files in my policies/active_admin folder

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

end

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!

解决方案

I found the answer!

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"

I had to add this to dashboard.rb under app/admin/dashboard.rb

def index
  authorize :dashboards, :index?
end

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

That got it working!

这篇关于登录后如何使Active Admin与Pundit一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆