Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6551683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:22:04+00:00 2026-05-25T12:22:04+00:00

In Michael Hartl’s Ruby on Rails Tutorial, ch 7.2.3 , rspec is returns the

  • 0

In Michael Hartl’s Ruby on Rails Tutorial, ch 7.2.3, rspec is returns the following errors:

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 <top (required)>'

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 <top (required)>'

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

In the console I’m also getting an undefined local variable error on ‘password confirmation’

I’ve thoroughly checked my code and can’t find the discrepancy but I’m obviously doing it wrong.

here’s my users model:

require 'digest'

class User < 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   => true,
                :length     => { :maximum => 50 }   
  validates :email, :presence   => true,
                :format     => { :with => email_regex },
                :uniqueness => { :case_sensitive => false }

  # Automatically create the virtual attribute 'password_confirmation'.
  validates :password, :presence     => true,
                   :confirmation => true,
                   :length       => { :within => 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:22:05+00:00Added an answer on May 25, 2026 at 12:22 pm

    Make sure this bit is in your spec, sounds like it’s missing

    before(:each) do
      @user = User.create!(@attr)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the Ruby on Rails tutorial by Michael Hartl http://ruby.railstutorial.org/ I installed
(ruby 1.9.2, rails 3.1) Following Michael Hartl's tutorial. Spent more than a day trying
I'm following Ruby on Rails Tutorial: Learn Rails by Example, by Michael Hartl. http://ruby.railstutorial.org/chapters/a-demo-app#sec:a_micropost_microtour
I am following Michael Hartl's Rails Tutorial Here: http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#top I use this command to
I just started reading Michael Hartl's Ruby on Rails 3 Tutorial. He recommends using
In section 6.2.4 of Ruby on Rails 3 Tutorial , Michael Hartl describes a
Im doing Michael Hartl's excellent tutorial series on ruby on rails here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book and
I making my Admin User using Michael Hartl's Rails 3 Tutorial .I am making
Currently doing the Ruby On Rails Tutorial by Michael Hartl, and am starting on
I'm working through Michael Hartl's Ruby on Rails tutorial on http://ruby.railstutorial.org . I'm having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.