Rails管理员与巫术

Dav*_*ite 4 ruby ruby-on-rails ruby-on-rails-3 rails-admin sorcery

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

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

config.authenticate_with do
  warden.authenticate! :scope => :admin
end
config.current_user_method { current_admin }
Run Code Online (Sandbox Code Playgroud)

我猜测在块中我需要引用before_filterSorcery用来验证用户的身份require_login.

但是,当我尝试这个并且我/admin在登出时尝试访问时,我收到路由错误:

No route matches {:action=>"new", :controller=>"sessions"}
Run Code Online (Sandbox Code Playgroud)

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

如何正确设置?

Dav*_*ite 6

# 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
Run Code Online (Sandbox Code Playgroud)