ActionMailer正在从控制台工作,但不是从控制器工作

时间:2015-05-25 06:51:15

标签: ruby-on-rails-4

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调用打印日志并正确发送电子邮件。请建议。

1 个答案:

答案 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

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")
  end
end
相关问题