首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Sorcery管理Rails

使用Sorcery管理Rails
EN

Stack Overflow用户
提问于 2012-03-22 08:59:03
回答 2查看 2.5K关注 0票数 4

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

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

代码语言:javascript
复制
config.authenticate_with do
  warden.authenticate! :scope => :admin
end
config.current_user_method { current_admin }

我猜在代码块中,我需要引用Sorcery用来验证用户身份的before_filter,它应该是require_login

但是,当我尝试这样做并且在注销时尝试访问/admin时,我得到一个路由错误:

代码语言:javascript
复制
No route matches {:action=>"new", :controller=>"sessions"}

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

我怎样才能正确地设置它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-23 14:06:03

代码语言:javascript
复制
# 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
票数 7
EN

Stack Overflow用户

发布于 2016-01-25 22:58:22

如果你使用魔法和Cancancan gem,你也应该在你的config/initializers/rails_admin.rb文件中添加config.current_user_method(&:current_user),否则你会得到一个错误:You are not authorized

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9815062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档