ruby-on-rails – 如何在同一用户模型中使用Devise和ActiveAdmin
发布时间:2020-12-16 21:46:39 所属栏目:百科 来源:网络整理
导读:我有ActiveAdmin和Devise与用户合作.我想使用Devise登录具有相同用户模型的常规非管理员用户.我该怎么做? (我想在用户模型中只有管理员拥有一个管理员标志.)我尝试将第二行添加到routes.rb devise_for :users,ActiveAdmin::Devise.configdevise_for :users
我有ActiveAdmin和Devise与用户合作.我想使用Devise登录具有相同用户模型的常规非管理员用户.我该怎么做? (我想在用户模型中只有管理员拥有一个管理员标志.)我尝试将第二行添加到routes.rb
devise_for :users,ActiveAdmin::Devise.config devise_for :users 但是当我尝试列出路由时,会出现错误 >rake routes DL is deprecated,please use Fiddle rake aborted! ArgumentError: Invalid route name,already in use: 'new_user_session' You may have defined two routes with the same name using the `:as` option,or you may be overriding a route already defined by a resource with the same naming. For the latter,you can restrict the routes created with `resources` as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created 我创建了一个授权适配器,只需检查user.admin == true即可正常使用ActiveAdmin. https://github.com/activeadmin/activeadmin/blob/master/docs/13-authorization-adapter.md 解决方法
我发现这个
http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin-and-devise/
但我最终这样做了
的routes.rb devise_for :admin_users,{class_name: 'User'}.merge(ActiveAdmin::Devise.config) ActiveAdmin.routes(self) devise_for :users resources :users application_controller.rb def access_denied(exception) redirect_to root_path,alert: exception.message end 配置/初始化/ active_admin.rb config.authorization_adapter = ActiveAdminAdapter config.on_unauthorized_access = :access_denied (并将所有方法从_user更改为admin_user.) 应用程序/模型/ active_admin_adapter.rb class ActiveAdminAdapter < ActiveAdmin::AuthorizationAdapter def authorized?(action,subject = nil) user.admin == true end end 和 rails generate migration add_admin_to_users admin:boolean (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |