在 Ruby on Rails 中使用 Devise 播种用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12418584/
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
Seeding users with Devise in Ruby on Rails
提问by at.
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 my db/seeds.rb file:
在我的开发和测试环境中,我想用一群用户为数据库做种子。我正在使用 Ruby on Rails v3.2.8 和最新的 Devise。所以我在我的 db/seeds.rb 文件中添加了这一行:
User.create(email: '[email protected]', encrypted_password: '#$taawktljasktlw4aaglj')
However, when I run rake db:setup, I get the following error:
但是,当我运行时rake db:setup,出现以下错误:
rake aborted! Can't mass-assign protected attributes: encrypted_password
耙子中止!无法批量分配受保护的属性:encrypted_password
What is the proper way to seed users?
播种用户的正确方法是什么?
回答by Arun Kumar Arjunan
You have to do like this:
你必须这样做:
user = User.new
user.email = '[email protected]'
user.encrypted_password = '#$taawktljasktlw4aaglj'
user.save!
Read this guide to understand what mass-assignment is: http://guides.rubyonrails.org/security.html
阅读本指南以了解什么是批量赋值:http: //guides.rubyonrails.org/security.html
I am wondering why do have to directly set the encrypted password. You could do this:
我想知道为什么要直接设置加密密码。你可以这样做:
user.password = 'valid_password'
user.password_confirmation = 'valid_password'
回答by George
Arun is right. It's easier just to do this in your seeds.rb
阿伦是对的。在你的seeds.rb 中更容易做到这一点
user = User.create! :name => 'John Doe', :email => '[email protected]', :password => 'topsecret', :password_confirmation => 'topsecret'
回答by Muhammad Sannan Khalid
The skip_confirmationmethod will only work if you have confirmablemodule in your user model, otherwise remove it.
该skip_confirmation方法仅confirmable在您的用户模型中有模块时才有效,否则将其删除。
user = User.new(
:email => "[email protected]",
:password => "123456",
:password_confirmation => "123456"
)
user.skip_confirmation!
user.save!
回答by Koxzi
This is an old question but here is an example with an admin user (from cancancan):
这是一个老问题,但这里有一个管理员用户的例子(来自 cancancan):
User.create!([
{email: "[email protected]", password: "testadminuser", password_confirmation: "testadminuser", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-02-06 14:02:10", last_sign_in_at: "2015-02-06 14:02:10", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", admin: true},
{email: "[email protected]", password: "testuseraccount", password_confirmation: "testuseraccount", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-02-06 14:03:01", last_sign_in_at: "2015-02-06 14:03:01", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", admin: false},
{email: "[email protected]", password: "testcustomeruser", password_confirmation: "testcustomeruser", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2015-02-06 14:03:44", last_sign_in_at: "2015-02-06 14:03:44", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", admin: false}
])
回答by Victor
If you're using the devise confirmablemodule you need to do something like:
如果您使用的是设计confirmable模块,则需要执行以下操作:
user = User.new(
email: '[email protected]',
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_atuser attribute as Time.now.utcbefore save.
该skip_confirmation!呼叫只是告诉制定,你并不需要确认这个账户。
其他选项只是confirmed_at像Time.now.utc保存之前一样设置用户属性。
回答by alexventuraio
I don't know if it may help but actually I'm doing this to create a default Admin userin my Rails 5app but without sharing the password in plain text in my GitHub repository.
我不知道它是否有帮助,但实际上我这样做是为了在我的Rails 5应用程序中创建一个默认的管理员用户,但没有在我的 GitHub 存储库中以纯文本形式共享密码。
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 linkin that default email account.
- Set a new password.
- 播种时为默认用户生成安全随机密码。
- 转到“.../admins/sign_in”并单击“忘记密码?” 链接重置它。
- 在该默认电子邮件帐户中获取重置密码链接。
- 设置新密码。
So in the db/seeds.rbappend this:
所以在db/seeds.rb追加这个:
randomPassword = Devise.friendly_token.first(8)
mainAdminUser = Admin.create!(email: "[email protected]", password: randomPassword, name: "Username")
And if you are using devise confirmablefeature just skip confirmable option by doing this:
如果您正在使用设计confirmable功能,只需通过执行以下操作跳过可确认选项:
mainAdminUser = Admin.new(
email: "[email protected]",
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.
现在您有一个默认用户,但您没有共享默认密码!我希望它对某人有用。
回答by doncadavona
To seed the users table:
要为 users 表设定种子:
User.create(
email: "[email protected]",
password: "12345678"
)
With deviseinstalled, the :passwordwill be automatically hashed and saved to :encrypted_password
随着色器件安装后,:password会自动散列并保存到:encrypted_password
回答by lacostenycoder
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.
对于seeds.rb 文件中的设计用户,对我有用的是在保存新用户时使用通用密码。然后更新加密密码并再次保存模型。这是一种骇人听闻的方式。
user = User.new(
:email => "[email protected]",
: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: "[email protected]",
password: "foob1234",
password_confirmation: "foob1234"
)
user.skip_confirmation! #only if using confirmable in devise settings in user model.
user.save!
回答by aloucas
Use your db/seeds.rb file to initiate your first user:
使用您的 db/seeds.rb 文件启动您的第一个用户:
User.create!(email: '[email protected]',
password: '123456789',
password_confirmation: '123456789')
回答by AnkitG
I Did something same in one of my requirements so just pasting my snippet
我在我的一项要求中做了同样的事情,所以只是粘贴我的代码片段
def triggerSeedUsers
p "Starting Seeding Users..."
p "Deleting all users"..
User.destroy_all
normal_users = [{:email => '[email protected]', :login => "abc_demo", :name => 'abc Demo'}]
admin_users = [{:email => '[email protected]', :login => 'abc_admin', :name => 'abc Admin'}]
[normal_users,admin_users].each do |user_type|
user_type.each do |user|
User.create!(:name => user[:name],
:login => user[:login],
:email => user[:email],
:first_login => false,
:password => 'P@ssw0rd',
:password_confirmation => 'P@ssw0rd'
)
end
end
User.where('name LIKE ?', '%demo%').update_all(:is_admin => 0)
User.where('name LIKE ?', '%admin%').update_all(:is_admin => 1)
end

