gpt4 book ai didi

ruby-on-rails - 如何将Devise和ActiveAdmin用于同一用户模型?

转载 作者:行者123 更新时间:2023-12-03 13:56:16 26 4
gpt4 key购买 nike

我有ActiveAdmin和Devise与用户一起工作。我想使用Devise使用相同的用户模型登录常规的非管理员用户。我怎样才能做到这一点? (我想在用户模型中仅对管理员使用admin标志。)我尝试将第二行添加到route.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/

但是我最终这样做了


设计3.4.1
ActiveAdmin 1.0.0.pre1
导轨4.2.1


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


config/initializers/active_admin.rb

config.authorization_adapter = ActiveAdminAdapter
config.on_unauthorized_access = :access_denied


(并将所有方法从 _user更改为 admin_user。)

app/models/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

关于ruby-on-rails - 如何将Devise和ActiveAdmin用于同一用户模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com