undefined 方法 'company_name' for nil:NilClass Rails 邮件程序视图不从控制器呈现变量



我设置了一个任务,用于检查截止日期尚未完成的所有后续工作。我现在已经发送了一个任务,该任务将运行并检查当天未完成的后续行动,并发送电子邮件提醒。我认为这一切都在起作用,但我无法在电子邮件中显示值,它一直给我一个NilClass错误。

rake中止!nil:NilClass 的未定义方法"company_name"

这个任务我现在正在运行rake,因为它将运行Cron(无论何时宝石),这一切都在工作。

提前感谢代码低于

lib/tasks/daily.rake

namespace :notifications do
  desc "Sends notifications"
  task :send => :environment do
    Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
      FollowupMailer.followup_confirmation(@followup).deliver  
    end
  end
end

followup_mailer.rb

class FollowupMailer < ActionMailer::Base
  default :from => "from@email.com"
  def followup_confirmation(followup)
    @followup = followup
    mail(:to => 'my@email.com', :subject => "Follow up Required")
  end
end

following_confirmation.text.erb

Good Day
Please action this follow up.
<%= @followup.company_name %>
Kind Regards
Mangement

错误源位于这个rake任务中:

namespace :notifications do
  desc "Sends notifications"
  task :send => :environment do
    Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
      FollowupMailer.followup_confirmation(@followup).deliver  
    end
  end
end

您正在尝试使用未设置的@followup实例变量。相反,您应该使用传递到块中的u

namespace :notifications do
  desc "Sends notifications"
  task :send => :environment do
    Followup.where(:closed => false, :quotefdate => (8640.hours.ago..Time.now)).each do |u|
      FollowupMailer.followup_confirmation(u).deliver # use u variable here
    end
  end
end

最新更新