2012-03-22 84 views
3

我试图安装Rails Admin Gem使用Sorcery进行身份验证而不是Devise。Rails admin with Sorcery

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

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

我猜测,块内我需要引用before_filter该巫用来验证用户身份,这将是require_login

然而,当我尝试,我尝试访问/admin登出时,我得到一个路由错误:

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

这可能是因为我被发动机内重定向,而不是在主应用程序。

如何正确设置?

回答

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 
0

如果使用巫术与Cancancan宝石,你也应该在你的config/initializers/rails_admin.rb文件中添加config.current_user_method(&:current_user),否则你会得到错误:You are not authorized