通过用户循环发送电子邮件

时间:2017-04-03 21:25:15

标签: ruby-on-rails ruby-on-rails-5

我尝试遍历管理员用户并向他们发送所有电子邮件,但我收到以下错误:undefined method "email" for nil:NilClass。这就是我循环的方式,我认为这是问题的根源:

def send_manager_email(current_user)
  managers = User.where(manager: true)
  managers.each do |manager|
    UserMailer.timesheet_notification(@manager, current_user).deliver_now
  end
end

这是我的邮件:

def timesheet_notification(user, current_user)
  @greeting = "Hi"
  @current_user = current_user
  mail to: user.email, subject: "New Timesheet"
end

我哪里错了?

1 个答案:

答案 0 :(得分:1)

请试试这个。您不需要@manager,您只能在传递参数时使用manager

def send_manager_email(current_user)
      managers = User.where(manager: true)
      managers.each do |manager|
        UserMailer.timesheet_notification(manager, current_user).deliver_now
      end
    end

这是我的邮件:

def timesheet_notification(user, current_user)
  @greeting = "Hi"
  @current_user = current_user
  mail to: user.email, subject: "New Timesheet"
end

希望这对你有用。谢谢!