从 Ruby 控制台创建一个设计用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4316940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a devise user from Ruby console
提问by Martin
Any idea on how to create and save a new User object with devise from the ruby console?
关于如何使用来自 ruby 控制台的设计创建和保存新用户对象的任何想法?
When I tried to save it, I'm getting always false. I guess I'm missing something but I'm unable to find any related info.
当我试图保存它时,我总是错误的。我想我错过了一些东西,但我找不到任何相关信息。
回答by jspooner
You can add false to the save method to skip the validations if you want.
如果需要,您可以向 save 方法添加 false 以跳过验证。
User.new({:email => "[email protected]", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" }).save(false)
Otherwise I'd do this
否则我会这样做
User.create!({:email => "[email protected]", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" })
If you have confirmablemodule enabled for devise, make sure you are setting the confirmed_atvalue to something like Time.nowwhile creating.
如果您confirmable为设计启用了模块,请确保在创建时将confirmed_at值设置为类似的值Time.now。
回答by Sam Ritchie
You should be able to do this using
您应该能够使用
u = User.new(:email => "[email protected]", :password => 'password', :password_confirmation => 'password')
u.save
if this returns false, you can call
如果返回 false,您可以调用
u.errors
to see what's gone wrong.
看看出了什么问题。
回答by akbarbin
When on your model has :confirmable option this mean the object user should be confirm first. You can do two ways to save user.
当您的模型具有 :confirmable 选项时,这意味着应首先确认对象用户。您可以通过两种方式保存用户。
a. first is skip confirmation:
一种。首先是跳过确认:
newuser = User.new({email: '[email protected]', password: 'password', password_confirmation: 'password'})
newuser.skip_confirmation!
newuser.save
b. or use confirm! :
湾 或使用确认!:
newuser = User.new({email: '[email protected]', password: 'password', password_confirmation: 'password'})
newuser.confirm!
newuser.save
回答by Flavio Wuensche
If you want to avoid sending confirmation emails, the best choice is:
如果你想避免发送确认邮件,最好的选择是:
u = User.new({
email: '[email protected]',
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.
因此,如果您使用的是假电子邮件或没有互联网连接,这将避免错误。
回答by Ezequiel Ramiro
None of the above answers worked for me.
以上答案都不适合我。
This is what I did:
这就是我所做的:
User.create(email: "[email protected]", password: "asdasd", password_confirmation: "asdasd")
Keep in mind that the password must be bigger than 6 characters.
请记住,密码必须大于 6 个字符。

