I have in my Person model a attribute named "hash" which is a hash of a
password and a salt.
When a user want to create a account of update it, he just have to
specify its password in a password virtual attribute. To do so, I use
this code:
# in model
class Person < ActiveRecord::Base
validates_length_of :password, :within => 6..40
def password
end
end
# in view
<p>Password <%= f.password_field :password %></p>
But when I try to create a account for testing, I get an error message
from the model that said my password is so short (less then 6 chars)...
even if my password got more chars. I don't understand why.
Think about what happens: the validation runs, and tries to get the password to see if you've set it to at least 6 characters. It calls the password method, which returns nil so the validation fails. Your password accessor needs to return the value set by the form (you could just use attr_accessor).
Think about what happens: the validation runs, and tries to get the
password to see if you've set it to at least 6 characters. It calls
the password method, which returns nil so the validation fails. Your
password accessor needs to return the value set by the form (you could
just use attr_accessor).
Fred
Thank you for your answer. By using attr_accessor :password, my password
attribute can be read and write.
But I have a thin other problem because of my special write attribute:
def password=(password)
self.salt = salt = Digest::SHA1.hexdigest(Time.now.to_s)
self.shadow = Digest::SHA1.hexdigest(password + salt)
end
Do you think there is a way to solve it by modify this method and just
remplace attr_accessor :password by attr_reader :password?
Think about what happens: the validation runs, and tries to get the
password to see if you've set it to at least 6 characters. It calls
the password method, which returns nil so the validation fails. Your
password accessor needs to return the value set by the form (you
could
just use attr_accessor).
Fred
Thank you for your answer. By using attr_accessor :password, my
password
attribute can be read and write.
But I have a thin other problem because of my special write attribute:
def password=(password)
self.salt = salt = Digest::SHA1.hexdigest(Time.now.to_s)
self.shadow = Digest::SHA1.hexdigest(password + salt)
end
Do you think there is a way to solve it by modify this method and just
remplace attr_accessor :password by attr_reader :password?
Well you need the password method to work, so something like
def password=(password)
@password = password
# rest of your method here
end