# Re: save question

**URL:** <https://www.ruby-forum.com/t/re-save-question/108193>\
**Category:** Rails\
**Created:** [June 29, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/re-save-question/108193 "2007-06-29T22:36:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Bradley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f0a364/32.png) [@Jonathan\_Bradley](https://www.ruby-forum.com/u/Jonathan_Bradley)\
**Post date:** [June 29, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/re-save-question/108193/1 "2007-06-29T22:36:22Z")

</div>

Ok i need to add a lost password feature. i need to generate a random  
password, then hash it and then save the hashed password and in turn  
mail  
then their new password.

here is the code i was trying to use, but having issues with it working  
properly:

# Lost Password

def lost\_password

```
if request.post?

  if user = User.find_by_email(params[:email])

    # Create new password

    chars = ("a".."z").to_a + ("1".."9").to_a

    @newpass = Array.new(8, 

```

‘’).collect{chars[rand(chars.size)]}.join

```
    @password = Digest::SHA1.hexdigest(@newpass)

    User.save = User.password

    flash[:notice] = "A new password has been emailed to you."

    Mailer.send(Mailer.new_password, @newpass)

    redirect_to login_url

  else

    flash[:notice] = "Please enter a valid email address"

  end

else

  flash[:notice] = ""

end

```

end
