Seeding users with Devise in Ruby on Rails

后端 未结 14 2437
终归单人心
终归单人心 2020-12-14 00:59

In my development and test environments, I want to seed the database with a bunch of users. I\'m using Ruby on Rails v3.2.8 and the latest Devise. So I added this line in

14条回答
  •  抹茶落季
    2020-12-14 01:34

    I don't know if it may help but actually I'm doing this to create a default Admin user in my Rails 5 app but without sharing the password in plain text in my GitHub repository.

    Basically the logic for this is:

    • Generate a secure random password for the default user when seeded.
    • Go to ".../admins/sign_in" and click on "Forgot your password?" link to RESET it.
    • Get the reset password link in that default email account.
    • Set a new password.

    So in the db/seeds.rb append this:

    randomPassword = Devise.friendly_token.first(8)
    mainAdminUser = Admin.create!(email: "me@gmail.com", password: randomPassword, name: "Username")
    

    And if you are using devise confirmable feature just skip confirmable option by doing this:

    mainAdminUser = Admin.new(
        email: "me@gmail.com",
        password: randomPassword,
        password_confirmation: randomPassword,
        name: "Username"
    )
    mainAdminUser.skip_confirmation!
    mainAdminUser.save!
    

    And your good to go!

    Now you have a default user but you are not sharing a default password! I hope it could be useful for somebody.

提交回复
热议问题