# Updating select attributes on a profile page

**URL:** <https://www.ruby-forum.com/t/updating-select-attributes-on-a-profile-page/153967>\
**Category:** Rails\
**Created:** [November 24, 2008, 3:51am UTC](https://www.ruby-forum.com/t/updating-select-attributes-on-a-profile-page/153967 "2008-11-24T03:51:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bobnation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Bobnation](https://www.ruby-forum.com/u/Bobnation)\
**Post date:** [November 24, 2008, 3:51am UTC](https://www.ruby-forum.com/t/updating-select-attributes-on-a-profile-page/153967/1 "2008-11-24T03:51:06Z")

</div>

Thanks ahead of time for everyone who will help out with this.

I have a profile page for a blog engine I’m writing (because everyone  
much write a blog engine at some point to learn stuff) and I want to  
allow members the ability to update their passwords. What I’m having a  
problem with right now is using the edit page for their profile to  
update their email and name (which is already populated from the  
database), which works, but also use it to update the password (which  
does not … because it updates the password to blank every time).

The situation is this: I am both salting and hashing the password, and  
using the attr\_accessor to grab the password they input on the  
registration page. However, now when they update their profile, the  
blank password field generates a new hash and salt and leaves the  
password blank. Not ideal.

I’m wondering how I might be able to selectively update the password  
if a new password is supplied … but leave it alone if a password is  
not supplied.

Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![Bobnation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Bobnation](https://www.ruby-forum.com/u/Bobnation)\
**Post date:** [November 24, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/updating-select-attributes-on-a-profile-page/153967/2 "2008-11-24T21:59:03Z")

</div>

I did a really kludgy fix last night using attr\_protected, and I like  
certain aspects of your idea as well. If I find some time tonight I’ll  
probably take a look at the code again to try and work out a more  
“beautiful” option for myself. 🙂

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![Bobnation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/41988e/32.png) [@Bobnation](https://www.ruby-forum.com/u/Bobnation)\
**Post date:** [November 24, 2008, 6:54pm UTC](https://www.ruby-forum.com/t/updating-select-attributes-on-a-profile-page/153967/3 "2008-11-24T18:54:36Z")

</div>

I use some code like this:

attr\_accessor :new\_password

validates\_presence\_of :new\_password, :on =\> :create  
validates\_length\_of :new\_password, :minimum =\> 6, :allow\_nil =\>  
true, :allow\_blank =\> true, :unless =\> :new\_password.blank?  
validates\_confirmation\_of :new\_password

before\_save :set\_encrypted\_password

def set\_encrypted\_password  
write\_attribute(:password, Client.encrypt\_password(@new\_password))  
unless @new\_password.nil? || @new\_password.blank?  
end

def self.encrypt\_password(password)  
Digest::SHA1.hexdigest(“something\_random\_goes\_here\_#{password}”)  
end

def self.authenticate(email, password)  
find\_by\_email\_and\_password\_and\_active(email, encrypt\_password  
(password), true)  
end
