Rails_admin 和专家:#<RailsAdmin::MainController 的未定义方法`policy' [英] Rails_admin and pundit: undefined method `policy' for #<RailsAdmin::MainController
问题描述
我在 rails 5 上,我正在尝试使用专家为我的 rails_admin 面板实施授权.所以我在我的应用程序控制器中包含了 Pundit 并安装了 rails_admin_pundit gem,正如你在我的这个片段中看到的宝石文件:
gem '设计'宝石设计-i18n"gem 'rails_admin', '~>1.0'宝石'rails_admin-i18n'宝石rails_admin_tag_list",github:kryzhovnik/rails_admin_tag_list"宝石专家"gem "rails_admin_pundit", :github =>sudosu/rails_admin_pundit"
申请政策:
class ApplicationPolicyattr_reader :current_user, :recorddef 初始化(当前用户,记录)@user = current_user@record = 记录结尾定义索引?错误的结尾定义显示?scope.where(:id => record.id).exists?结尾定义创建?错误的结尾定义新?创造?结尾定义更新?错误的结尾定义编辑?更新?结尾def销毁?错误的结尾def rails_admin?(动作)案件诉讼什么时候:仪表板@user.admin?什么时候:索引@user.admin?什么时候:显示@user.admin?什么时候:新@user.admin?什么时候:编辑@user.admin?什么时候:销毁@user.admin?什么时候:出口@user.admin?什么时候:历史@user.admin?当:show_in_app@user.admin?别的引发 ::Pundit::NotDefinedError,无法为 #{record} 找到策略 #{action}."结尾结尾结尾
还有一段我的 rails_admin 初始值设定项:
RailsAdmin.config 做 |config|config.authorize_with :punditconfig.current_user_method(&:current_user)...结尾
所以现在当我加载管理仪表板(网址:/admin")时,我收到此错误:
<块引用>#RailsAdmin::MainController:0x0055914e2523a0 的未定义方法`policy'>
我按照所有说明进行操作,但我仍然看不到缺少什么.任何答案/建议将不胜感激.
这也困扰着我.最终发现:https://travis-ci.org/sferik/rails_admin/jobs/152180750>
rails_admin 现在默认为 ::ActionController::Base
要修复,请将以下内容放入 rails_admin.rb:
config.parent_controller = '::ApplicationController'
I'm on rails 5 and I'm trying to implement authorizations with pundit for my rails_admin panel. So I included pundit in my application controller and installed the rails_admin_pundit gem as you can see in this snippet of my Gemfile:
gem 'devise'
gem 'devise-i18n'
gem 'rails_admin', '~> 1.0'
gem 'rails_admin-i18n'
gem 'rails_admin_tag_list', github: 'kryzhovnik/rails_admin_tag_list'
gem 'pundit'
gem "rails_admin_pundit", :github => "sudosu/rails_admin_pundit"
The application policy:
class ApplicationPolicy
attr_reader :current_user, :record
def initialize(current_user, record)
@user = current_user
@record = record
end
def index?
false
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 rails_admin?(action)
case action
when :dashboard
@user.admin?
when :index
@user.admin?
when :show
@user.admin?
when :new
@user.admin?
when :edit
@user.admin?
when :destroy
@user.admin?
when :export
@user.admin?
when :history
@user.admin?
when :show_in_app
@user.admin?
else
raise ::Pundit::NotDefinedError, "unable to find policy #{action} for #{record}."
end
end
end
And a snippet of my rails_admin initializer:
RailsAdmin.config do |config|
config.authorize_with :pundit
config.current_user_method(&:current_user)
...
end
So now when I load the admin dashboard (url: "/admin") I get this error:
undefined method `policy' for #RailsAdmin::MainController:0x0055914e2523a0>
I followed all the instructions, but I still don't see what's missing. Any answer/suggestion will be greatly appreciated.
This bugged me too. Eventually found: https://travis-ci.org/sferik/rails_admin/jobs/152180750
rails_admin now defaults to ::ActionController::Base
To fix, put the following in rails_admin.rb:
config.parent_controller = '::ApplicationController'
这篇关于Rails_admin 和专家:#<RailsAdmin::MainController 的未定义方法`policy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!