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
The skip_confirmation
method will only work if you have confirmable
module in your user model, otherwise remove it.
user = User.new(
:email => "admin@xxxxx.xxx",
:password => "123456",
:password_confirmation => "123456"
)
user.skip_confirmation!
user.save!
with email confirmating in 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")
Just add :password attribute rest, devise will do password_encrypt for you
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
and now run 'rake db:seed'
Arun is right. It's easier just to do this in your seeds.rb
user = User.create! :name => 'John Doe', :email => 'john@gmail.com', :password => 'topsecret', :password_confirmation => 'topsecret'
You have to do like this:
user = User.new
user.email = 'test@example.com'
user.encrypted_password = '#$taawktljasktlw4aaglj'
user.save!
Read this guide to understand what mass-assignment is: 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'
This is an old question but here is an example with an admin user (from 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}
])