Sent from my iPhone

On Apr 10, 2011, at 6:29 AM, Misha Ognev <[email protected]> wrote:

> 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 =>
> "[email protected]", :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 =>
> "[email protected]", :password => "radjahhhh")
> => #<User id: 1, name: "misha", email: "[email protected]", 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.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to