我使用rails 3.2和ruby 1.9.3并设计2.1.2。
我希望管理员使用用户名和电子邮件创建用户。
如何为该用户随机生成密码,并向他发送包含密码的电子邮件?
答案 0 :(得分:1)
首先,您需要通过admin为句柄注册用户创建控制器。
假设您的控制器名称为registrations_controller.rb
这是app/controller/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
before_filter :resource_name
def resource_name
:user
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
generated_password = Devise.friendly_token.first(6) #password length 6
@user.password_confirmation = @user.password = generated_password
if @user.save
# Send Password Via Email
UserMailer.password_send(@user).deliver
redirect_to a_path
else
render action: "new"
end
end
这是app/mailer/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "youremail@email.com"
def password_send(user)
@user = user
mail(:to => user.email, :subject => "password", :from => "youremail@email.com")
end
end
这是app/views/user_mailer/password_send.html.erb
Email : @user.email<br/>
Username : @user.username<br/>
Password : @user.password
Devise - Automatically generate password for users
希望这有帮助!
答案 1 :(得分:0)
为用户随机生成密码
在您的控制器中
pwd = Devise.friendly_token[0,16]
@user = User.new(params[:user])
@user.password_confirmation = @user.password = pwd
发送包含密码的电子邮件
看看这个问题:Send an actionmailer email upon user registration in devise rails 3 application
最终它将归结为:
mail(:to => "#{user.email}", :subject => "Welcome to My Awesome Site, your password #{pwd}")