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?
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)
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/validations.rb:49:in
`save!'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/attribute_methods/dirty.rb:30:in
`save!'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/transactions.rb:245:in
`block in save!'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/transactions.rb:292:in
`block in with_transaction_returning_status'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in
`transaction'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in
`transaction'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in
`with_transaction_returning_status'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/transactions.rb:245:in
`save!'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/activerecord-3.0.5/lib/active_record/validations.rb:34:in
`create!'
from (irb):1
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in
`start'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in
`start'
from
/usr/local/rvm/gems/ruby-1.9.2-p180@basicdev/gems/railties-3.0.5/lib/rails/commands.rb:23:in
`<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
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>
Thanks in advance, Misha
--
Posted via http://www.ruby-forum.com/.
--
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.