I'm going through the railstutorial.org book and getting Unknown attribute error in code
My User.rb file code is
# == 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
#
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
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 }
before_save { |user| user.email = email.downcase }
end
and my user_spec.rb file code is
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com" , 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)}
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
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.
foo#bar_baz.com foo#bar+baz.com]
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[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
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
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
end
and what is going in terminal is
ritesh#ritesh-desktop:~/projects/sample_app$ rails console
Loading development environment (Rails 3.2.9)
1.9.3p327 :001 > User.create(name: "Michael Hartl", email: "mhartl#example.com", password: "foobar", password_confirmation: nil)
ActiveRecord::UnknownAttributeError: unknown attribute: password
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:88:in `block in assign_attributes'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:78:in `each'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/base.rb:497:in `initialize'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/persistence.rb:44:in `new'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/persistence.rb:44:in `create'
from (irb):1
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/ritesh/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.3p327 :002 >
please give some clue how to run rectify this error!!
Add a column by generating a migration:
rails generate migration AddPasswordToUsers password:string
Then run:
rake db:migrate
However, it looks like you're attempting to use has_secure_password, though you haven't added it to your User model. If this is the case, add the line has_secure_password to your User model and run this migration instead:
rails generate migration AddPasswordDigestToUsers password_digest:string
I faced this same issue when working on a Ruby on Rails application with a PostgreSQL database in production.
Here's how I solved it:
The issue was that I added some new columns to the table(s) in my development environment using migration files, but when I made a push to the production environment, I didn't create those new columns via migration.
All I had to do was to simply run a database migration in the production environment.
rails db:migrate
That's all.
I hope this helps
Related
I've been following along Hartl's book at rubystutorial.org and stuck on this error
When I run rspec tests it tells me that the method (save) does not exist for user
Here's my code and test. I can't find what's wrong with it and why the method doesn't exist within #user
user_spec.rb
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
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 "shoud be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo. foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to 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 = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
end
And my user.rb
class User < ActiveRecord::Base
before_save { self.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 } )
end
Exact error is
Failures:
1) User when email address is already taken
Failure/Error: user_with_same_email.save
NoMethodError:
undefined method `save' for "USER#EXAMPLE.COM":String
# ./spec/models/user_spec.rb:55:in `block (3 levels) in <top (required)>'
Finished in 0.411 seconds
24 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:58 # User when email address is already take
You are defining a user and then change it on string (email, so email has no save method) in the next lines:
user_with_same_email = #user.dup
user_with_same_email = #user.email.upcase
user_with_same_email.save
You should change it for this:
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
Object
The problem here can be seen with the error itself:
undefined method `save' for "USER#EXAMPLE.COM":String
The problem you have is not that .save is unavailable, but that the object you're trying to call it on is unable to process it.
The problem is specifically to do with this line:
user_with_same_email = #user.email.upcase
--
Fix
The issue is you're setting the user_with_same_email variable as #user.email.upcase. You can do what zishe suggested (to set the email attribute of the new variable)
I don't really understand what you're trying to achieve - as your User model sets the email to downcase on the before_save callback.
I'm currently working on 6.3.3 of Hartl's tutorial http://www.railstutorial.org/book/modeling_users
after running rspec spec/ I'm getting the following failures - I'm sure I'm misunderstanding the instructions or something. I'm grateful for any insight into what I'm doing wrong:
leo$ rspec spec/
............FFF...................
Failures:
1) User return value of authenticate method with invalid password
Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:92:in `block (4 levels) in <top (required)>'
# ./spec/models/user_spec.rb:95:in `block (4 levels) in <top (required)>'
2) User return value of authenticate method with invalid password
Failure/Error: it { should_not eq user_for_invalid }
NameError:
undefined local variable or method `user_for_invalid' for # <RSpec::Core::ExampleGroup::Nested_2::Nested_10::Nested_2:0x007faed251c830>
# ./spec/models/user_spec.rb:94:in `block (4 levels) in <top (required)>'
3) User return value of authenticate method with valid password
Failure/Error: it { should eq found_user.authenticate(#user.password) }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:88:in `block (4 levels) in <top (required)>'
Finished in 0.24185 seconds
34 examples, 3 failures
Failed examples:
rspec ./spec/models/user_spec.rb:95 # User return value of authenticate method with invalid password
rspec ./spec/models/user_spec.rb:94 # User return value of authenticate method with invalid password
rspec ./spec/models/user_spec.rb:88 # User return value of authenticate method with valid password
here is what I have in my user_spec.rb file:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
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) }
it { should respond_to(:authenticate) }
it { should be_valid }
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. foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-er#f.b.org frst.1st#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to 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.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com", password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.password) }
describe "with valid password" do
it { should eq found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid }
specify { expext(user_for_invalid_password).to be_false }
end
end
end
and here's what I have in my user.rb file:
class User < ActiveRecord::Base
before_save { self.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 }
has_secure_password
validates :password, length: { minimum:6 }
end
Found your typo:
describe "return value of authenticate method" do
# should find by #user.email
let(:found_user) { User.find_by(email: #user.password) }
end
I can't for the life of me seem to figure out why these three tests aren't passing. I'm pretty new with rails but I feel as though I've tried about everything, at least according to the tutorial. I'm sure its something obvious but my attempts to find an answer have been in vain. My failed tests are as follows(pulled from Michael Hartl's Rails Tutorial). Thanks.
Failures:
1) User when password is not present return value of authenticate method with valid password
Failure/Error: it { should eq found_user.authenticate(#user.password) }
NoMethodError:
undefined method authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:94:inblock (5 levels) in '
2) User when password is not present return value of authenticate method with invalid password
Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }
NoMethodError:
undefined method authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:98:inblock (5 levels) in '
# ./spec/models/user_spec.rb:100:in `block (5 levels) in '
3) User when password is not present return value of authenticate method with invalid password
Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }
NoMethodError:
undefined method authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:98:inblock (5 levels) in '
# ./spec/models/user_spec.rb:101:in `block (5 levels) in '
User_spec tests
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
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) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
it "should be valid" do
expect(#user).to 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.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to 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.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
#user = User.new(name:"Example User", email: "user#example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
describe "when password does not match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password thats too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
end
end
User model
class User < ActiveRecord::Base
before_save { self.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 }
has_secure_password
validates :password, length: { minimum: 6 }
end
You are getting the error as found_user is nil.
Thats because you are not closing when password is not present describe block properly.
So what happens is, this invalid #user is set with a blank password and its been carried forward till your failing test case.
describe "when password is not present" do
before do
#user = User.new(name:"Example User", email: "user#example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end ### Add end here
Also, remove the very last end from the bottom of the file.
My problem is when I try to use the rails console to update a user email I get NoMethodError: undefined method 'update_attribute'
While working thru the problem I tried
current_user = user.authrnticate(foobar) and got back NameError: undefined local variable or method 'user' for main:Object
Which leads me to believe the "rails c" isn't using or seeing the user.rb file corectly.
I have tried restarting the rails server before and after performing [rake db:test:prepare] and [rake db:migrate].
My [rails c] will execute methods not defined in user.rb.
Rspec is running without a problem. All tests in the user_spec are passing. and when I comment out has_secure_password all tests fail.
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
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) }
it { should respond_to(:authenticate) }
it { should be_valid }
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 to 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.
foo#bar_baz.com foo#bar+baz.com]
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[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
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.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation " do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
describe "when password is too short" do
before { #user.password = #user.password_confirmation = "a" * 5}
it { should_not be_valid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
My user.rb is
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = user.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, presence: true, length: {minimum: 6 }
validates :password_confirmation, presence: true
end
and here is the entire project on github
I have looked exhaustively for a solution including a throe read of the ActiveRecord documentation and I'm stumped.
Any help is greatly appreciated.
Many of the helper methods are not available to be called in the console.
you can often access them by calling them via helper.helpername, but this doesn't always work
How are you getting the User from the database?
For example user = User.find_by_name('name')
This error:
NameError: undefined local variable or method 'user' for main:Object
Says that you didn't instantiate a variable user.
Did you fetch User from a database in to user variable?
Your irb session should look something like this:
To check if you have a user in database:
> User.all
If you have a valid user in database named "Jon"
> user = User.find_by_name("Jon")
> current_user = user.auhenticate("valid_password") #will work
If you don't have a user in database:
> User.create(name: "Jon", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
> user = User.find_by_name("Jon")
> current_user = user.auhenticate("valid_password") #will work
RSpec uses test database but rails c on default uses development one so make sure that you have a record in your development database when you want to work on it in rails c.
rails c command is loading all your project libraries.
I am following the Ruby on Rails tutorial by Michael Hartl. My tests were all passing till I migrated the database and added the last 2 test. Something got messed up since all the tests are failing now even if I remove the last 2 tests. I am not sure if I made some mistake with the migrate or something else.
This is what I used:
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
$ bundle exec rspec spec/
Here's the user_spec file which fails completely:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com", 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) }
it { should be_valid }
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. foo#bar_baz.com foo#bar_baz.com]
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[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
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.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
end
And here is the migration file:
class AddPassswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
EDIT2 - I just removed password: "foobar", password_confirmation: "foobar" from the first line of the spec and now I only get 5 errors (which is right as those related to password should fail)
EDIT1 - Here are the failing tests
Running all specs
Running tests with args ["--drb", "-f", "progress", "-r", "/home/niranjan/.rvm/gems/ruby-1.9.3-p194/gems/guard-rspec-1.2.1/lib/guard/rspec/formatters/notification_rspec.rb", "-f", "Guard::RSpec::Formatter::NotificationRSpec", "--out", "/dev/null", "--failure-exit-code", "2", "spec"]...
..FFFFFFFFFFFFFFF.........
Failures:
1) User
Failure/Error: #user = User.new(name: "Example User", email: "user#example.com", password: "foobar", password_confirmation: "foobar")
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:16:in `new'
# ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'
2) User
Failure/Error: #user = User.new(name: "Example User", email: "user#example.com", password: "foobar", password_confirmation: "foobar")
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:16:in `new'
# ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'
3) User
Failure/Error: #user = User.new(name: "Example User", email: "user#example.com", password: "foobar", password_confirmation: "foobar")
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:16:in `new'
# ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'
4) User
Failure/Error: #user = User.new(name: "Example User", email: "user#example.com", password: "foobar", password_confirmation: "foobar")
ActiveRecord::UnknownAttributeError:
unknown attribute: password
# ./spec/models/user_spec.rb:16:in `new'
# ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'
.... and so on
From the book:
Putting everything together gives the (failing) tests in Listing 6.28. We’ll get them to pass in Section 6.3.4.
The tests should be failing. They will pass as soon as the password implementation is done, later on the tutorial.
Now, to explain why they are failing:
On the line where you create the user (your before block), you are passing the password attribute to the constructor, but that attribute doesn't exist yet, so your model complains with the errors you are seeing. On the tutorial this attribute is created a little later.