Reputation: 263
I am followed Michael Hartl's http://ruby.railstutorial.org until Chapter 6.3.2 where I stuck at Password and Confirmation. ( http://ruby.railstutorial.org/chapters/modeling-users#sec:adding_a_secure_password )
I have added "attr_assessor :password, :password_confirmation' to User.rb from what I understand after reading the instruction stated below:
"As seen in the mockup in Figure 6.1, we expect to have users confirm their passwords, a common practice on the web meant to minimize typos. We could enforce this at the controller layer, but it’s conventional to put it in the model and use Active Record to enforce the constraint. The method is to add password and password_confirmation attributes to the User model, and then require that the two attributes match before the record is saved to the database. Unlike the other attributes we’ve seen so far, the password attributes will be virtual—they will only exist temporarily in memory, and will not be persisted to the database."
I get 11 failures on bundle exec guard on user_spec.rb after adding:
password: "foobar", password_confirmation: "foobar" to @User.new in models/user_spec.rb
Something like "virtual" attributes (password, password_confirmation) that do not exist in db:development.sqlite3. This is what I am trying to do with no success. I even tried all possible methods such as hashes with @User in user.rb
What did I do wrong here?
Thanks kindly in advance
file: spec/user_spec.rb
require 'spec_helper'
describe User do
before do
# @user = User.new(name: "Example User", email: "[email protected]")
@user = User.new(name: "Example User", email: "[email protected]",
password: "foobar", password_confirmation: "foobar")
end
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) }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
addresses.each do |invalid_address|
@user.email = invalid_address
@user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[[email protected] [email protected] [email protected] [email protected]]
addresses.each do |valid_address|
@user.email = valid_address
@user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.save
end
it { should_not be_valid }
end
end
.
file: models/user.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
# password_digest :string(255)
#
class User < ActiveRecord::Base
attr_accessor :password, :password_confirmation
attr_accessible :email, :name
# attr_accessible :email, :name
# attr_accessible :email, :name, :password, :password_confirmation
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
#validates :password, length: { minimum: 6 }
#validates :password_confirmation, presence: true
end
Upvotes: 1
Views: 1195
Reputation: 21
Adding has_secure_password to the model fixes the problem. It's in the book (later part) too.
Upvotes: 2
Reputation: 754
If you are only on section 6.3.2 then the tests should be failing. You will be correcting that in the following sections.
Upvotes: 3
Reputation: 146
I would guess the errors are complaining about "Could not find table 'users'" Try:
rake db:migrate
rake db:load:test
Upvotes: 0