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

前端 未结 2 1243
春和景丽
春和景丽 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:16

    You just define role.rb first by creating migratioin

          rails g model role  name:string
    

    then in role.rb

          class Role
             has_one:user
          end
    

    And in user model

          class user
             belongs_to :role
          end
    

    Insert two roles into DB

       1.admin
       2.user
    

    Then check by this

        if user.role.name == "admin"  
            # can do all your logic
        else
             your logic
        end
    

    Make sure insert role_id:integer into user model

    Try it.......

提交回复
热议问题