1

I currently have in my application the ability for a user to create an account with a username and password and then have an email sent to them.

In order to generate a more secure log in, I wish for a random password to be generated and then in the email sent to the user the password is contained there.

I am just wondering how I would do this. I have a password_hash and salt in my database so it would require having the random string assigned there.

I understand that I would have to have in my model something like

 before_save :assign_password

and then a

def assign_password
     stuff
end

Is this all I would need and it would assign to the password field? what would I include in the def assign_password?

Jak
  • 2,893
  • 3
  • 19
  • 33
user1222136
  • 553
  • 2
  • 6
  • 17
  • possible duplicate of [Randomly generated password Rails 3.1](http://stackoverflow.com/questions/9066245/randomly-generated-password-rails-3-1) – Andrew Grimm Sep 14 '15 at 08:49

1 Answers1

-1
def assign_password
  (0..6).map{ ('a'..'z').to_a[rand(26)] }.join
end

Source How to generate a random string in Ruby

Community
  • 1
  • 1
mohamagdy
  • 2,093
  • 2
  • 17
  • 22