ruby on rails - No method error - save from rubystutorial.org -



ruby on rails - No method error - save from rubystutorial.org -

i've been next along hartl's book @ rubystutorial.org , stuck on error

when run rspec tests tells me method (save) not exist user

here's code , test. can't find what's wrong , why method doesn't exist within @user

user_spec.rb

require 'spec_helper' describe user before @user = user.new(name: "example user", email: "user@example.com") end subject { @user } { should respond_to(:name) } { should respond_to(:email) } { should be_valid } describe "when name not present" before { @user.name = " " } { should_not be_valid } end describe "when email not present" before { @user.email = " " } { should_not be_valid } end describe "when name long" before { @user.name = "a" * 51 } { should_not be_valid } end describe "when email format invalid" "shoud invalid" addresses = %w[user@foo,com user_at_foo.org example.user@foo. foo@bar_baz.com foo@bar+baz.com] addresses.each |invalid_address| @user.email = invalid_address expect(@user).not_to be_valid end end end describe "when email format valid" "should valid" addresses = %w[user@foo.com a_us-er@f.b.org frst.lst@foo.jp a+b@baz.cn] addresses.each |valid_address| @user.email = valid_address expect(@user).to be_valid end end end describe "when email address taken" before user_with_same_email = @user.dup user_with_same_email = @user.email.upcase user_with_same_email.save end { should_not be_valid } end end

and 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 taken failure/error: user_with_same_email.save nomethoderror: undefined method `save' "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 take

you defining user , alter on string (email, email has no save method) in next lines:

user_with_same_email = @user.dup user_with_same_email = @user.email.upcase user_with_same_email.save

you should alter this:

user_with_same_email = @user.dup user_with_same_email.email = @user.email.upcase user_with_same_email.save

ruby-on-rails ruby rspec save nomethoderror

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -