Creating an admin user in Devise on Rails beta 3

前端 未结 6 1206
梦谈多话
梦谈多话 2020-12-22 16:35

Ok, I\'m probably going to feel quite dumb when someone answers this one with a simple thing that I\'m missing but... here goes:

I\'ve got a brand new app on rails 3

6条回答
  •  生来不讨喜
    2020-12-22 17:30

    This may not apply to Devise (but I believe it will), but in general if you want to seed an admin user but don't want to store your admin password in source control, you can do something like this...

    @user = User.find_by_email("admin@email.com")
    
    unless @user
      # We are going to bypass both our assignment protection and validation
      # so we aren't storing the password in source control.
      #
      # This doesn't replace the need to change the password occasionaly, both
      # on the site and in source control.
      @user = User.create do |u|
        u.name = "Admin User"
        u.email = "admin@email.com"
        u.password_digest = "$2a$10$DUv/IUiLB34jhi3j4Z8MwwcaDlBmFe3rvcdXSzPKLzBOAMmD53UqW"
      end
    
      @user.save(:validate => false)
    
      # TODO make the user an admin
    end
    

    You can create the user locally with the password you want to find the password_digest.

提交回复
热议问题