How do I use Devise and ActiveAdmin for the same User model?

前端 未结 1 1416
礼貌的吻别
礼貌的吻别 2020-12-13 07:32

I have ActiveAdmin and Devise working with Users. I would like to use Devise to log in regular non-admin users with the same User model. How can I do this? (I want to have a

相关标签:
1条回答
  • 2020-12-13 07:52

    I found this http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin-and-devise/

    But I ended up doing this

    Devise 3.4.1
    ActiveAdmin 1.0.0.pre1
    Rails 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
    

    (And changing all methods from _user to admin_user.)

    app/models/active_admin_adapter.rb
    class ActiveAdminAdapter < ActiveAdmin::AuthorizationAdapter
      def authorized?(action, subject = nil)
        user.admin == true
      end
    end
    

    And

    rails generate migration add_admin_to_users admin:boolean
    
    0 讨论(0)
提交回复
热议问题