views:

32

answers:

2

I am getting a beautiful error :

failed with NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[] - 3 failed attempts

My Controller:

CardSignup.all.each do |user|
  Delayed::Job.enqueue MassEmail.new(user, params[:subject], params[:editor1])
end

mass_email.rb

class MassEmail < Struct.new(:user, :subject, :message)
  def perform
    Notifier.deliver_email_blast(user, subject, message)
  end
end

_form.html.haml

- form_tag admin_email_blast_path do
  Subject
  %br
  = text_field_tag 'subject'
  %br
  Body
  %br
  = text_area_tag 'message', '', :name => 'editor1'
  %br
  = submit_tag 'Send Email', :class => 'button'

  :plain
    <script type="text/javascript">
    CKEDITOR.replace( 'editor1' );
    </script>

The problem is because i'm not passing the varis right.. how do I do this proper?

note: It was working perfectly before i implemented delayed_jobs with params[:subject], and params[:editor1], so I have to retain those somehow.

I tried this without Delayed_Jobs, and the MassEmail.new doesn't even leave a backtrace in my log.

I also tried this in my console, and it froze.

There's got to be something wrong with the mass_email.rb or the call in def perform

+1  A: 

This is definitively wrong, you gave two parameters the same name:

class MassEmail < Struct.new(:user, :params, :params)

How does the perform method now which param you're referring to?

You can try something like this

class MassEmail < Struct.new(:user, :subject, :editor1)
  def perform
    Notifier.deliver_email_blast(user, subject, editor1)
  end
end
jigfox
Well at least its a step up in syntax. But it still returns the same error. I'd give a better trace, but its failing through delayed_jobs.
Trip
I think its erroring because the vars i'm calling from my form in my MassEmail.new can't be passed through ?
Trip
A: 

Kudos to JigFox for getting me to write the params right.

And I'm not entirely sure why this works, but I moved the loop into the MassEmail.new function.

Delayed::Job.enqueue MassEmail.new(params[:subject], params[:editor1])

class MassEmail < Struct.new(:subject, :editor1)
  def perform
    CardSignup.all.each do |user|
      Notifier.deliver_email_blast(user, subject, editor1)
    end
  end
end
Trip