I just can’t get validates_confirmation_of working for the life of
me!..
It’s not the first time I’m having problems with it but I just never
understand why!.. This time I pretty much followed exactly the
example in Agile Rails Development and still…it just won’t work! I put
2 different passwords in the form and it doesn’t trigger an error,
nothign…
Here is my user model:
require ‘digest/sha1’
class User < ActiveRecord::Base
belongs_to :profile
belongs_to :company
validates_presence_of :username, :email, :address1, :city, :state,
:zip
validates_uniqueness_of :username, :email
attr_accessor :password_confirmation
validates_confirmation_of :password
validates_format_of :email, :with => /^[_a-z0
-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0 -9-]+)(.[a-z]{2,4})$/i
def validate
errors.add_to_base(“Missing Password”) if hashed_password.blank?
end
def self.authenticate(username, password)
user = self.find_by_username(username)
if user
expected_password = encrypted_password(password, user.salt)
if user.hashed_password != expected_password
user = nil
end
end
user
end
def password
@password
end
def password=(pwd)
@password = pwd
create_new_salt
self.hashed_password = User.encrypted_password(self.password,
self.salt)
end
private
def self.encrypted_password(password, salt)
string_to_hash = password + “wibble” + salt
Digest::SHA1.hexdigest(string_to_hash)
end
def create_new_salt
self.salt = self.object_id.to_s + rand.to_s
end
end
As you can see besides from a couple additional form, it’s pretty much
an exact copy of the suggested method in Agile Web D. with
Rails…
This is (part) of my view:
<% form_for :user, :url => {:action => ‘join’} do |f| %>
…
Pick a Password
<%= f.password_field :password %>
Confirm Password<br>
<%= f.password_field :password_confirmation %>
<br><br>
…
<% end %>
Once again pretty straight forward…! What could be wrong?