我想使用rails_admin
gem保护sorcery
页面。根据此SO answer,执行此操作的方法如下:
# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
config.authenticate_with do
# Use sorcery's before filter to auth users
require_login
end
end
# app/controllers/application_controller.rb
class ApplicationController
# Overwrite the method sorcery calls when it
# detects a non-authenticated request.
def not_authenticated
# Make sure that we reference the route from the main app.
redirect_to main_app.login_path
end
end
这将覆盖巫术用于不登录的默认方法。覆盖确实可以在我的应用程序中工作,但是当我访问rails_admin页面时,出现以下错误:
undefined local variable or method `root_path' for #<RailsAdmin::MainController.
所以重写在rails_admin代码中不起作用。我在路径文件底部安装了rails_admin,
# config/routes.rb
...
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
我该如何解决?
答案 0 :(得分:0)
这是因为rails_admin控制器未从我的应用程序控制器继承。有一个rails_admin配置设置可设置此继承,即
#config/initializers/rails_admin.rb
RailsAdmin.config do |config|
...
config.parent_controller = 'ApplicationController'
end