Note that there are some explanatory texts on larger screens.

plurals
  1. PORails Tutorial ch 7.2.3 has_password? undefined method
    text
    copied!<p>In Michael Hartl's Ruby on Rails Tutorial, <a href="http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#sec%3aimplementing_has_password" rel="nofollow">ch 7.2.3</a>, rspec is returns the following errors:</p> <pre><code>Failures: 1) User has_password? method should be true if the passwords match Failure/Error: @user.has_password?(@attr[:password].should be_true) NoMethodError: undefined method `has_password?' for nil:NilClass # ./spec/models/user_spec.rb:132:in `block (3 levels) in &lt;top (required)&gt;' 2) User has_password? method should be false if the passwords don't match Failure/Error: @user.has_password?("invalid").should be_false NoMethodError: undefined method `has_password?' for nil:NilClass # ./spec/models/user_spec.rb:136:in `block (3 levels) in &lt;top (required)&gt;' Finished in 0.23931 seconds 18 examples, 2 failures Failed examples: rspec ./spec/models/user_spec.rb:131 # User has_password? method should be true if the passwords match rspec ./spec/models/user_spec.rb:135 # User has_password? method should be false if the passwords don't match </code></pre> <p>In the console I'm also getting an undefined local variable error on 'password confirmation'</p> <p>I've thoroughly checked my code and can't find the discrepancy but I'm obviously doing it wrong. </p> <p>here's my users model:</p> <pre><code>require 'digest' class User &lt; ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :name, :presence =&gt; true, :length =&gt; { :maximum =&gt; 50 } validates :email, :presence =&gt; true, :format =&gt; { :with =&gt; email_regex }, :uniqueness =&gt; { :case_sensitive =&gt; false } # Automatically create the virtual attribute 'password_confirmation'. validates :password, :presence =&gt; true, :confirmation =&gt; true, :length =&gt; { :within =&gt; 6..40 } before_save :encrypt_password def has_password?(submitted_password) self.encrypted_password == encrypt(submitted_password) end private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload