Rails管理员与巫术

我正在尝试使用Sorcery安装Rails Admin Gem进行身份validation而不是Devise。

Rails管理员确实提供了一个钩子,您可以使用它来附加您自己的身份validation方法。 以下是他们在文档中提供的示例(使用warden):

config.authenticate_with do warden.authenticate! :scope => :admin end config.current_user_method { current_admin } 

我猜测在块中我需要引用Sorcery用来validation用户的before_filter ,这将是require_login

但是,当我尝试这个并且我在注销时尝试访问/admin时,出现了路由错误:

 No route matches {:action=>"new", :controller=>"sessions"} 

这可能是因为我在引擎内而不是在主应用程序中重定向。

如何正确设置?

 # 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 #config/initializers/rails_admin.rb RailsAdmin.config do |config| ... config.parent_controller = 'ApplicationController' end 

如果您使用带有Cancancan gem的Sorcery,您还应该在config/initializers/rails_admin.rb文件中添加config.current_user_method(&:current_user) ,否则您将收到错误: You are not authorized