# Change Password

**URL:** <https://www.ruby-forum.com/t/change-password/104461>\
**Category:** Rails\
**Created:** [May 18, 2007, 10:51am UTC](https://www.ruby-forum.com/t/change-password/104461 "2007-05-18T10:51:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 10:51am UTC](https://www.ruby-forum.com/t/change-password/104461/1 "2007-05-18T10:51:50Z")

</div>

Hallo to all

I have a big Problem by changing the Password of user. I have a database  
with user and theier password and now i want to reset the Password!

admin\_controller.rb  
def restore\_user  
@user = User.find\_by\_id(params[:id])  
@user.deleted = false  
@user.save  
flash[:notice] =“Benutzer #{@user.username} wiederhergestellt”  
redirect\_to(:action =\> “list\_delete”)  
end

user.rb  
def password=(pwd)  
@password = pwd  
create\_new\_salt  
self.hashed\_password = User.encrypted\_password(self.password,  
self.salt)  
end

Please can somebody help me??

Thank you

Harald

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 18, 2007, 11:40am UTC](https://www.ruby-forum.com/t/change-password/104461/2 "2007-05-18T11:40:54Z")

</div>

Harald

> I have a big Problem by changing the Password of user.

You didn’t say what your proble is!  
Anyway, here is - working - code I use to reset the user’s lost  
password.

## in the users\_controller.rb

def generate\_and\_send\_new\_password  
…  
@user = User.find\_by\_login(params[:login])  
@user.password\_confirmation = @user.password = generate\_password

flash[:notice] = “new password sent to #{@user.email}”  
PasswordMailer.deliver\_new\_password( @user, @user.password,  
login\_url+“?login=#{@user.login}”)  
render :action =\> ‘password\_changed’  
end

# src:

[http://www.rubyonrailsblog.com/articles/2006/09/29/ruby-on-rails-random-password-generator](http://www.rubyonrailsblog.com/articles/2006/09/29/ruby-on-rails-random-password-generator)  
def generate\_password(length = 6)  
chars = (‘a’…‘z’).to\_a + (‘A’…‘Z’).to\_a + (‘1’…‘9’).to\_a -  
[‘o’, ‘O’, ‘i’, ‘I’]  
Array.new(length) { chars[rand(chars.size)] }.join  
end  
end

HTH

## Alain R.

[http://blog.ravet.com](http://blog.ravet.com)
