nil:NilClass Rails 邮件程序视图的未定义方法 `company_name' 未从控制器呈现变量

undefined method `company_name' for nil:NilClass Rails mailer views not rendering variables from controller

我已经设置了一个任务来检查所有在某个日期之前未完成的跟进。我现在已经发送了一个任务,该任务将 运行 并检查当天是否有未完成的跟进并发送电子邮件提醒。我认为一切正常,但我无法在电子邮件本身中显示值,它一直给我一个 NilClass 错误。

耙子中止! nil:NilClass

的未定义方法“company_name”

这个任务我运行目前正在通过rake进行,因为它将运行通过Cron(每当gem)进行,一切正常。

提前致谢代码如下

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

followup_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