Rails 4: can't save a record to db

2k Views Asked by At

I've got a problem: i'm trying to add authentication to my first rails app using (mac os x mavericks): rails 4, mysql (via mysql2 gem), ActiveRecord. Following notes on railstutorial guide I've added: Gemfile:

gem 'bcrypt-ruby', '~> 3.1.2'

User model:

class User < ActiveRecord::Base
    has_secure_password
    attr_accessible :lastname, :firstname, :email, :password
end

Create users migration:

class CreateUsers < ActiveRecord::Migration
def change
    create_table :users do |t|
        t.string :lastname
        t.string :firstname
        t.string :email
        t.string :password_digest
    end
end
end

Then I'm trying to create and save new user using rails console (because I havent created views and controller):

user = User.create(lastname: "LastName", firstname: "Name", email: "mail.mail.com", password: "qwerty")
user.save

and there is an error message on my console:

(0.3ms)  BEGIN
(0.2ms)  ROLLBACK

Hope someone could explain me what's wrong and how to get readable error why my transaction was rollbacked

1

There are 1 best solutions below

1
On BEST ANSWER

Examine user.errors in the console, and you'll see that you're missing the required password_confirmation attribute (necessary when creating new records using has_secure_password)

See the example in the documentation -- the user doesn't save in that example until the confirmation matches the password.