Search code examples
ruby-on-railsrubypasswordsauthlogicrails-generate

How to automaticly generate password if not provided with authlogic


I'm new on both this site and ruby on rails! I have a common installation of authlogic and want password to be generated automaticly for user registration if user did not set the password. What is the best way to do it?


Solution

  • The solution is:

    #app/models/User.rb
    class User < ActiveRecord::Base
    #...
      before_validation_on_create { |record| record.randomize_password unless record.password }
    #...
    end