validates :confirmation don't work

Hello, I have an ruby 1.9.2 with rails 3.0.5. I'm doing
railstutorial.org, but have a problem: validations of confirmation don't
work! What's matter?

Here is my user.rb model file:

# == Schema Information
# Schema version: 20110408112831

Hello, I have an ruby 1.9.2 with rails 3.0.5. I'm doing
railstutorial.org, but have a problem: validations of confirmation don't
work! What's matter?

Here is my user.rb model file:

# == Schema Information
# Schema version: 20110408112831
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255)
# salt :string(255)

require 'digest'
class User < ActiveRecord::Base
attr_accessible :name, :email, :encrypted_password, :salt, :password
attr_accessor :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 }
validates :password, :presence => true, :length => {:within => 6..40},
:confirmation => true

#def has_password?(submitted_password)
  # encrypted_password == encrypt(submitted_password)
#end

#def self.authenticate(email, submitted_password)
  # user = find_by_email(email)
   #user && user.has_password?(submitted_password) ? user : nil
#end

private

def encrypt_password
  self.salt = make_salt if new_record?
  self.encrypted_password = encrypt(self.password)
end

def encrypt(string)
secure_hash("#{string}--#{self.salt}")
end

def make_salt
secure_hash("#{Time.now.utc}--#{self.password}")
end

def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
end

When I try to save with small password, it's error, but when I forget,
it isn't. Why?

I don't understand your question. What do you mean "when you forget it isn't"?

ruby-1.9.2-p180 :001 > User.create!(:name => "misha", :email =>
"katya@mail.com", :password => "ghgh")
ActiveRecord::RecordInvalid: Validation failed: Password is too short
(minimum is 6 characters)

This is correct. You only have 4 characters in your password.

ruby-1.9.2-p180 :002 > User.create!(:name => "misha", :email =>
"radik@mail.com", :password => "radjahhhh")
=> #<User id: 1, name: "misha", email: "radik@mail.com", created_at:
"2011-04-10 11:29:13", updated_at: "2011-04-10 11:29:13",
encrypted_password: nil, salt: nil>

This is correct. Your password is 9 characters long.

What exactly is the issue here? It appears that validation is working exactly as it is supposed to.

B.

Bryan, I mean: why rails don't need confirmation of password?

Misha

http://edgeguides.rubyonrails.org/active_record_validations_callbacks.html#validates_confirmation_of

You are already asking in your model for it to validate the presence of the password, it’s length between 6-40 characters, and that it matches the password in the password_confirmation field. It’s all on this one line.

validates :password, :presence => true, :length => {:within => 6…40}, :confirmation => true

It only runs that validation if password_confirmation is not nil. Since you aren’t supplying a :password_confirmation in your create statement it is treated as nil therefore no check is run. You need to require the that the user give you password_confirmation. Add the following right after the above code line and that should fix your issue.

validates :password_confirmation, :presence => true

B.

Thanks! It works!