使用 rails_admin 滚动您自己的身份验证 gem 停止使用 rails 5

Roll your own authentication with the rails_admin gem stops working with rails 5

这个 shows how to authenticate the rails_admin gem when you have a roll your own authentication. The method follows this pattern。但是,此解决方案不再适用于 rails 5。相反,当尝试访问管理视图时,会在 config/initializers/rails_admin.rb

中触发以下异常
undefined method `signed_in?' for #<RailsAdmin::MainController:0x007fbe38628ab0>

你如何解决这个问题?

通常的视图助手,如 signed_in?current_user 在初始化器中不再可用,所以我想出的解决方案是:

config.authenticate_with do
    current_user =  User.find_by_id(session[:user_id])
    raise 'You must be admin' unless current_user.try(:admin?)
  end