re: save question

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

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