简体   繁体   中英

How to use sorcery gem with rails_admin

I would like to secure the rails_admin pages using the sorcery gem. According to this SO answer , the way to do this is as follows:

# 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

This overrides sorcery's default method for handling no login. The overriding does work in my app, but when I visit the rails_admin pages, I get the following error:

 undefined local variable or method `root_path' for #<RailsAdmin::MainController. 

so the overriding is not working in the rails_admin code. I am mounting rails_admin at the bottom of my routes file with

# config/routes.rb
...
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'

How do I fix this?

It is because the rails_admin controller is not inheriting from my application controller. There is a rails_admin configuration setting that sets this inheritance, ie

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM