2012-03-22 82 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