我正在尝试使用Sorcery而不是Devise来安装Rails Admin Gem进行身份验证。
Rails admin确实提供了一个钩子,您可以使用它附加您自己的身份验证方法。Here是他们在文档中提供的示例(使用典狱长):
config.authenticate_with do
warden.authenticate! :scope => :admin
end
config.current_user_method { current_admin }我猜在代码块中,我需要引用Sorcery用来验证用户身份的before_filter,它应该是require_login。
但是,当我尝试这样做并且在注销时尝试访问/admin时,我得到一个路由错误:
No route matches {:action=>"new", :controller=>"sessions"}这可能是因为我在引擎中被重定向,而不是在主应用程序中。
我怎样才能正确地设置它?
发布于 2012-03-23 14:06:03
# 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发布于 2016-01-25 22:58:22
如果你使用魔法和Cancancan gem,你也应该在你的config/initializers/rails_admin.rb文件中添加config.current_user_method(&:current_user),否则你会得到一个错误:You are not authorized。
https://stackoverflow.com/questions/9815062
复制相似问题