Seeding users with Devise in Ruby on Rails

后端 未结 14 2390
终归单人心
终归单人心 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.

    0 讨论(0)
  • 2020-12-14 01:36

    Use your db/seeds.rb file to initiate your first user:

    User.create!(email: 'palo@alto.com', 
                 password: '123456789', 
                 password_confirmation: '123456789')
    
    0 讨论(0)
  • 2020-12-14 01:37

    To seed the users table:

    User.create(
            email: "example@gmail.com",
            password: "12345678"
        )
    

    With devise installed, the :password will be automatically hashed and saved to :encrypted_password

    0 讨论(0)
  • 2020-12-14 01:40

    If you're using the devise confirmable module you need to do something like:

    user = User.new(
      email: 'user@domain.com', 
      password: '123456789', 
      password_confirmation: '123456789'
    )
    user.skip_confirmation!
    user.save!
    

    The skip_confirmation! call just tell to devise that you don't need to confirm this account.
    Other option is just set the confirmed_at user attribute as Time.now.utc before save.

    0 讨论(0)
  • 2020-12-14 01:40

    For devise users in seeds.rb file, what worked for me was to use a generic password upon saving the new user. Then update the encrypted password and save the model again. This was a hacky way.

    user = User.new(
        :email                 => "admin@xxxxx.xxx",
        :password              => "fat12345",
        :password_confirmation => "fat12345"
    )
    user.save!
    user.encrypted_password="ENCRYPT.MY.ASS!!!KJASOPJ090923ULXCIULSH.IXJ!S920"
    user.save
    

    UPDATE others have posted and this is better way to do it:

    user = User.new(
        email: "foo@bar.com", 
        password: "foob1234", 
        password_confirmation: "foob1234"
    )
    user.skip_confirmation! #only if using confirmable in devise settings in user model.
    user.save!
    
    0 讨论(0)
  • 2020-12-14 01:40

    This allows you to run a seed multiple times without errors:

    User.where(email: "you@email.com").first_or_create.update_attributes(nome: "Your Name",
        email: "you@email.com",
        password:              "password#123",
        password_confirmation: "password#123")
    
    0 讨论(0)
提交回复
热议问题