def create
@user = User.find_by_email(params[:forgotpassword][:email])
Rails.logger.info "User: #{@user.inspect}"
if @user
@a=new_random_password
@user.attributes={:temp_password=>@a}
if @user.save(:validate=>false)
byebug
if Emailer.forgot_password_email(@user)
flash[:success] = "Mail sent to #{params[:forgotpassword][:email]}"
redirect_to fplast_path
else
flash[:error] = 'Email sending failed'
redirect_to(:back)
end
else
flash[:error] = 'Sorry!There is some problem. We can\'t process your email now.'
redirect_to(:back)
end
else
flash[:error] = 'This Email id is not registered. Please try with another or Sign up'
redirect_to(:back)
end
end
这是我的控制器代码,它调用以下方法发送电子邮件。
#emailer.rb
class Emailer < ActionMailer::Base
default from: "from address", :charset => "UTF-8"
def forgot_password_email(user)
puts "HERE"
Rails.logger.info "HERE"
@user = user
Rails.logger.info "Call came here at least"
mail(:to => user.email, :subject => "Reset password").deliver
end
end
控制器中的日志似乎正在打印,但emailer.rb
中的日志未打印。但是,从rails控制台,Emailer.forgot_password_email
调用打印日志并正确发送电子邮件。请建议。
答案 0 :(得分:1)
def create
@user = User.find_by_email(params[:forgotpassword][:email])
Rails.logger.info "User: #{@user.inspect}"
if @user
@a=new_random_password
@user.attributes={:temp_password=>@a}
if @user.save(:validate=>false)
if Emailer.forgot_password_email(@user).deliver_now
flash[:success] = "Mail sent to #{params[:forgotpassword][:email]}"
redirect_to fplast_path
else
flash[:error] = 'Email sending failed'
redirect_to(:back)
end
else
flash[:error] = 'Sorry!There is some problem. We can\'t process your email now.'
redirect_to(:back)
end
else
flash[:error] = 'This Email id is not registered. Please try with another or Sign up'
redirect_to(:back)
end
end
class Emailer < ActionMailer::Base
default from: "from address", :charset => "UTF-8"
def forgot_password_email(user)
puts "HERE"
Rails.logger.info "HERE"
@user = user
Rails.logger.info "Call came here at least"
mail(:to => user.email, :subject => "Reset password")
end
end