Why is the virtual attribute being set to null?
I am working on a ruby on rails signup/signin system.
I have a ruby on rails form which has a virtual attribute - password. The action in response to form submission updates the model object and saves it. Before saving the before_save: callback calls the encrypt_password method. However, I don't think that the virtual attribute is getting the value from the form. This is because when I try signing in it fails repeatedly.The code is:
attr_accessor :password
attr_accessible :Name, :password, :UserName, :RegistrationName, :BusinessName
before_save :encrypt_password
def encrypt_password
self.salt = make_salt
self.encrypted_password=encrypt(password)
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
Ok,I checked and yes the password obtained from the form is null. But I don't understand why. This is the code for the form:
<h1>This is the page of <%= @merchant.Name %> </h1>
<p>Please enter the password to be used </p>
<%= random_generator %>
<%= form_for @merchant,:url=>{:action=>'approve'} do |m| %>
<%= m.label :Name %><br/>
<%= m.text_field :Name %><br/>
<%= m.label :password %><br/>
<%= m.text_field :password %><br/>
<%= m.submit "Approve" %>
<% end %>
Whatever text is being entered in the password 开发者_JS百科field, is visible in the Approve action in the controller, if I print it directly with params[:merchant][:password]. But in the model it is null
like any attr_accessor
, you can get it's value with:
instance.password
Put some debugger
line where you find it relevant and have a look.
精彩评论