Rails : Adding an admin role using devise who can see all the users

前端 未结 2 1241
春和景丽
春和景丽 2021-01-21 00:33

I need to create an admin role using devise for my app. I\'ve created basic authentication using devise . I have a devise user model in my app but now i need an admin who can sh

2条回答
  •  不知归路
    2021-01-21 01:15

    I have similar requirement as yours and also don't want any user to be able to signup. All that will be taken care by Admin. Her what I've done.

    I added another devise model called Admin

    rails generate devise MODEL
    

    Disable 'Registerable' for User model so that user cannot singup by themself

    user.rb

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :registerable,    :timeoutable and :omniauthable
      devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
    
      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name,  :last_name, :role, :admin
      # attr_accessible :title, :body
    end
    

    Enable CRUD for user by using sample from here: https://gist.github.com/1056194

    Finally protect protect the users controller like so

    users_controller.rb

    # Add this
    before_filter :authenticate_admin!
    

    Hope this helps.

提交回复
热议问题