try this:
def lost_password
if request.post?
@user = User.find_by_email(params[:email]) if params[:email]
if @user
# 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.password = @password
@user.save
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
end
end
It would be my recommendation to move the password encryption to the model
but this should work for you.
-Adam
On 6/29/07, Jonathan Bradley
<jonathan-/GIlNnT6J+gTR8g7N4ExuEEOCMrvLtNR@public.gmane.org>
wrote:>
> 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
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---