Well, very frustrated with this one because I am following the example
straight from the documentation and it is not working.
First when I try:
validates_confirmation_of :password, :on => :password_update
(password_update is a def in my User model which works fine without
this validation in please)
I get the following error:
TypeError in UsersController#update_password
nil is not a symbol
[Full trace at the end of this message.]
However, if I do only this:
validates_confirmation_of :password
It jumps over the validation because no matter what I put in the
password and password_confirmation fields, changes are always saved.
I thought the :on option could take any def from the controller and
apply the validation only when that action was called. But now I see
it only takes :save, :create and :update.
However, even when putting :update as the :on option, it is still
bypassing the validation even though the data in the fields are not
equal.
And one correction in my previous post, password_update is a def in my
controller, not in my model, like I stated previously.
I thought the :on option could take any def from the controller and
apply the validation only when that action was called. But now I see
it only takes :save, :create and :update.
It's not a controller thing (how would the model know the action that
was causing it to be saved), it's a model thing: is the instance
being saved for the first time or all the time
However, even when putting :update as the :on option, it is still
bypassing the validation even though the data in the fields are not
equal.
However, I think the problem has to do with this, used for the
encryption of the password.
def password=(value)
if !value.blank?
write_attribute("password", Digest::SHA1.hexdigest(value))
end
end
Because I got it to the point that the validation worked, but it
always returned that the values didn't match.
When I took out the lines for encryption it worked perfectly. So it
seems it looks to compare the password string encrypted with the
password_confirmation string not encrypted.