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
=>
"katya-O5WfVfzUwx8@public.gmane.org", :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
=>
"radik-O5WfVfzUwx8@public.gmane.org", :password =>
"radjahhhh")
=> #<User id: 1, name: "misha", email:
"radik-O5WfVfzUwx8@public.gmane.org", 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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Sent from my iPhone On Apr 10, 2011, at 6:29 AM, Misha Ognev <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 => > "katya-O5WfVfzUwx8@public.gmane.org", :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-O5WfVfzUwx8@public.gmane.org", :password => "radjahhhh") > => #<User id: 1, name: "misha", email: "radik-O5WfVfzUwx8@public.gmane.org", 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bryan, I mean: why rails don''t need confirmation of password? 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Apr 10, 2011 at 11:38 AM, Misha Ognev <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Bryan, I mean: why rails don''t need confirmation of password? > >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. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks! It works! -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.