Clayton
Clayton

Reputation: 3

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

I have setup a Task that check for all the followups that are outstanding by a date. I have now send up a task that will run and check for outstanding follow ups for the day and send out an email reminder. All working i think but i can't get the values to show in the Email itself it keep giving me a NilClass error.

rake aborted! undefined method `company_name' for nil:NilClass

This task i am running through rake at the moment as it will be running through Cron (Whenever gem) which all is working.

Thanks in Advance Code is Below

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 => "[email protected]"

  def followup_confirmation(followup)
    @followup = followup
    mail(:to => '[email protected]', :subject => "Follow up Required")

  end

end

followup_confirmation.text.erb

Good Day

Please action this follow up.

<%= @followup.company_name %>

Kind Regards

Mangement

Upvotes: 0

Views: 242

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

The error source is located in this rake task:

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

You're trying to use @followup instance variable, which is unset. Instead, you should use u passed into block:

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

Upvotes: 1

Related Questions