如何在rails_admin中使用魔法宝石

时间:2018-10-22 21:22:11

标签: ruby-on-rails sorcery

我想使用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'

我该如何解决?

1 个答案:

答案 0 :(得分:0)

这是因为rails_admin控制器未从我的应用程序控制器继承。有一个rails_admin配置设置可设置此继承,即

#config/initializers/rails_admin.rb
RailsAdmin.config do |config|
  ...
  config.parent_controller = 'ApplicationController'
end