在 rails 管理员中配置管理员

Configuring Admin in rails admin

我已经在我的应用程序上成功设置了 rails admin - 问题是现在任何人都可以访问 /admin ..我正在使用 devise,如何设置 admin 用户并强制他们登录?

我不确定如何实现

您可以为用户添加布尔属性 table 并为管理员设置为真,然后您可以使用 cancan gem https://github.com/ryanb/cancan.

使用https://github.com/CanCanCommunity/cancancan。适当调整权限。

ability.rb

user ||= User.new # guest user

if user.roles.include? :admin
  can :manage, :all
elsif user.roles.include? :editor
  can :access, :rails_admin   # grant access to rails_admin
  can :dashboard              # grant access to the dashboard
  can :index,       :all
  can :read,        :all
  can :export,      :all
  can :show_in_app, :all
  can :create,      []
  can :update,      []
  can :destroy,     []
  ...

app_controller.rb

rescue_from CanCan::AccessDenied do |exception|
  redirect_to main_app.root_path, alert: exception.message
end