Create a devise user from Ruby console

前端 未结 5 1341
广开言路
广开言路 2020-12-04 04:48

Any idea on how to create and save a new User object with devise from the ruby console?

When I tried to save it, I\'m getting always false. I guess I\'m missing some

相关标签:
5条回答
  • 2020-12-04 05:25

    If you want to avoid sending confirmation emails, the best choice is:

        u = User.new({
          email: 'demo@greenant.com.br',
          password: '12feijaocomarroz',
          password_confirmation: '12feijaocomarroz'
        })
    
        u.confirm
        u.save
    

    So if you're using a fake email or have no internet connection, that'll avoid errors.

    0 讨论(0)
  • 2020-12-04 05:27

    When on your model has :confirmable option this mean the object user should be confirm first. You can do two ways to save user.

    a. first is skip confirmation:

    newuser = User.new({email: 'superadmin1@testing.com', password: 'password', password_confirmation: 'password'})
    newuser.skip_confirmation!
    newuser.save
    

    b. or use confirm! :

    newuser = User.new({email: 'superadmin2@testing.com', password: 'password', password_confirmation: 'password'})
    newuser.confirm!
    newuser.save
    
    0 讨论(0)
  • 2020-12-04 05:36

    None of the above answers worked for me.

    This is what I did:

    User.create(email: "a@a.com", password: "asdasd", password_confirmation: "asdasd")
    

    Keep in mind that the password must be bigger than 6 characters.

    0 讨论(0)
  • 2020-12-04 05:40

    You should be able to do this using

    u = User.new(:email => "user@name.com", :password => 'password', :password_confirmation => 'password')
    u.save
    

    if this returns false, you can call

    u.errors
    

    to see what's gone wrong.

    0 讨论(0)
  • 2020-12-04 05:48

    You can add false to the save method to skip the validations if you want.

    User.new({:email => "guy@gmail.com", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" }).save(false)
    

    Otherwise I'd do this

    User.create!({:email => "guy@gmail.com", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" })
    

    If you have confirmable module enabled for devise, make sure you are setting the confirmed_at value to something like Time.now while creating.

    0 讨论(0)
提交回复
热议问题