Topic: Ruby on Rails Tutorial attribute not found

I'm following Michael Hartl's tutorial at  I got to the part where it creates a User instance.  The User model has the following attributes:
#  id              :integer(4)      not null, primary key
#  name            :string(255)
#  email           :string(255)
#  created_at      :datetime        not null
#  updated_at      :datetime        not null
#  password_digest :string(255)

password_digest is where the encrypted password is stored.  But then the author uses two variables, password and password_confirmation in the rspec test:
require 'spec_helper'

describe User do

  before do
    @user = "Example User", email: "",
                     password: "foobar", password_confirmation: "foobar")

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should be_valid }

He says password and password_confirmation are not necessary to store in the database, they are just created temporarily in memory.  But when I try to run this code I get an error telling me that password and password_confirmation are undefined.


Re: Ruby on Rails Tutorial attribute not found

ok, I found the solution but I have no idea how or why it works. I had to add has_secure_password to the User class.

Can anyone explain what this has_secure_password does?

thanks, mike

Re: Ruby on Rails Tutorial attribute not found

If I remember right has_secure_password is a helper function that you wrote in the sessions_helper. It checks if the password_digest is not empty. Remember that :password and :password confirmation are virtual attributes created by the model for the purpose of creating the hash and salt. They should be named at the top of your model under the attar_accessor property. Hope that helps.