[英]Seeding users with Devise in Ruby on Rails
在我的開發和測試環境中,我想用一群用戶為數據庫做種子。 我正在使用 Ruby on Rails v3.2.8 和最新的 Devise。 所以我在我的 db/seeds.rb 文件中添加了這一行:
User.create(email: 'test@example.com', encrypted_password: '#$taawktljasktlw4aaglj')
但是,當我運行rake db:setup
,出現以下錯誤:
耙子中止! 無法批量分配受保護的屬性:encrypted_password
播種用戶的正確方法是什么?
你必須這樣做:
user = User.new
user.email = 'test@example.com'
user.encrypted_password = '#$taawktljasktlw4aaglj'
user.save!
閱讀本指南以了解什么是批量賦值: http : //guides.rubyonrails.org/security.html
我想知道為什么要直接設置加密密碼。 你可以這樣做:
user.password = 'valid_password'
user.password_confirmation = 'valid_password'
阿倫是對的。 在你的seeds.rb 中更容易做到這一點
user = User.create! :name => 'John Doe', :email => 'john@gmail.com', :password => 'topsecret', :password_confirmation => 'topsecret'
skip_confirmation
方法僅在您的用戶模型中有可confirmable
模塊時才有效,否則將其刪除。
user = User.new(
:email => "admin@xxxxx.xxx",
:password => "123456",
:password_confirmation => "123456"
)
user.skip_confirmation!
user.save!
這是一個老問題,但這里有一個管理員用戶的例子(來自 cancancan):
User.create!([
{email: "testadmin@mvmanor.co.uk", 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: "testuser@mvmanor.co.uk", 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: "testcustomer@customer.co.uk", 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}
])
如果您正在使用設計可confirmable
模塊,您需要執行以下操作:
user = User.new(
email: 'user@domain.com',
password: '123456789',
password_confirmation: '123456789'
)
user.skip_confirmation!
user.save!
skip_confirmation!
打電話只是告訴設計你不需要確認這個帳戶。
其他選項只是在保存前將confirmed_at
用戶屬性設置為Time.now.utc
。
要為 users 表設定種子:
User.create(
email: "example@gmail.com",
password: "12345678"
)
隨着色器件安裝在:password
將自動散列並保存到:encrypted_password
我不知道它是否有幫助,但實際上我這樣做是為了在我的Rails 5應用程序中創建一個默認的管理員用戶,但沒有在我的 GitHub 存儲庫中以純文本形式共享密碼。
基本上,這樣做的邏輯是:
所以在db/seeds.rb
添加:
randomPassword = Devise.friendly_token.first(8)
mainAdminUser = Admin.create!(email: "me@gmail.com", password: randomPassword, name: "Username")
如果您正在使用設計可confirmable
功能,只需通過執行以下操作跳過可確認選項:
mainAdminUser = Admin.new(
email: "me@gmail.com",
password: randomPassword,
password_confirmation: randomPassword,
name: "Username"
)
mainAdminUser.skip_confirmation!
mainAdminUser.save!
祝你好運!
現在您有一個默認用戶,但您沒有共享默認密碼! 我希望它對某人有用。
使用您的 db/seeds.rb 文件啟動您的第一個用戶:
User.create!(email: 'palo@alto.com',
password: '123456789',
password_confirmation: '123456789')
對於seeds.rb 文件中的設計用戶,對我有用的是在保存新用戶時使用通用密碼。 然后更新加密密碼並再次保存模型。 這是一種駭人聽聞的方式。
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
更新其他人已經發布,這是更好的方法:
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!
我在我的一項要求中做了同樣的事情,所以只是粘貼我的代碼片段
def triggerSeedUsers
p "Starting Seeding Users..."
p "Deleting all users"..
User.destroy_all
normal_users = [{:email => 'abc@domain.com', :login => "abc_demo", :name => 'abc Demo'}]
admin_users = [{:email => 'admin@domain.com', :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
在 db:seed 中使用電子郵件確認:
User.create!( name: 'John', email:'john@hotmail.com', password: '123456', password_confirmation: '123456',confirmed_at: '2018-08-04 04:51:43', current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1")
只需添加 :password 屬性休息,設計將為您做 password_encrypt
user = User.new({ email: 'test@example.com', password: 'EnterYourPassword'})
user.save!
flash[:notice] = 'User Created'
#or for extra logic
#if user.save
#ExtraCredentialsOrLogic
#elsif user.errors.any?
#user.errors.full_messages.each do |msg|
#puts msg
#end
#else
#puts "****NOT VALID****"
#end
現在運行'rake db:seed'
這允許您多次運行種子而不會出錯:
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")
不要嘗試創建加密密碼,為您設計處理。 這將起作用,只需確保密碼長度至少為 6 個字符。
User.create(
email: "test@test.com",
password: "123456"
)
更好的是,在您的終端中:
$bundle add faker
然后:
User.create(
email: test@test.com,
password: "123456"
)
10.times do
User.create(
email: Faker::Internet.email,
password: "123456"
)
end
只要確保您在種子中設置了至少一封您能記住的電子郵件。 因此保留“測試”電子郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.