0

This is a total newbie question, but I wonder if someone could assist with setting up a mailer. I have a model 'users' and nested underneath it a 'contacts' model, (with a has_many/belongs_to relationship).

I am now trying to create a mailer which will be triggered by a specific action (create post) on a user page, and will email all the contacts belonging to that user. But I can't crack the syntax required on the mailer - I've tried setting recipients to @user.contacts.all and I've tried looping through them as with this solution. Can anybody advise on the cleanest way to do it?

Here's the code I have so far:

Posts controller:

  after_create :send_contact_email

  private

  def send_contact_email
    ContactMailer.contact_email(self).deliver
  end

contact_mailer (this is my latest attempt, taken from the RoR site - I suspect this is NOT the best way to do it...)

class ContactMailer < ActionMailer::Base 

  def contact_email(user)
    recipients    @user.contacts.all
    from          "My Awesome Site Notifications <notifications@example.com>"
    subject       "Welcome to My Awesome Site"
    sent_on       Time.now
    body          {}
  end
end

And then a basic message for the contact_email.html.erb.

The current error is:

NoMethodError in UsersController#create_post

undefined method `contacts' for nil:NilClass.

Any advice you can offer would be really gratefully received!

* Update *

Following Baldrick's advice, the contact_email method is now:

class ContactMailer < ActionMailer::Base 
  default :to => Contact.all.map(&:contact_email),
          :from => "notification@example.com"

  def contact_email(user)
    @user = user
    mail(:subject => "Post added")
  end
end
Community
  • 1
  • 1
Nick
  • 839
  • 1
  • 10
  • 19

2 Answers2

2

There's a typo: you are using @user instead of user in the contact_email method. It may not be the only problem, but at least it's the cause of the error message "undefined method 'contacts' for nil:NilClass "

Update

So with the right syntax, remove the :to from default options, and set it in the contact_emailmethod, with the contacts of your user:

class ContactMailer < ActionMailer::Base 
  default :from => "notification@example.com"

  def contact_email(user)
    @user = user
    mail(:subject => "Post added", :to => user.contacts.map(&:contact_email),)
  end
end
Baldrick
  • 23,882
  • 6
  • 74
  • 79
  • Even after changing it to `recipients user.contacts.all` I still get the same message, I'm afraid :-( – Nick Jan 30 '12 at 11:08
  • so maybe the value of `user` is also nil... Nevertheless, your method `contact_email` is not syntactically correct, I can't see what you're trying to do. Look at examples in http://guides.rubyonrails.org/action_mailer_basics.html to help you – Baldrick Jan 30 '12 at 11:16
  • Thank you - I was using an old version of the rails docs, it turns out. With the correct syntax (added above), I now have the `contact_email` method successfully emailing -- unfortunately it goes to all contacts in the db though, regardless of whether they belong to the user I'm adding the post to or not. Again, changing to `:to => User.contact.all.map(&:contact_email),` doesn't seem to work... – Nick Jan 30 '12 at 11:38
  • I've edited my answer, you need to move the `:to =>` option to the `contact_email` method – Baldrick Jan 30 '12 at 12:49
  • Thanks Baldrick - your new code works and sends emails, but it still includes the contacts which belong to another user :-( – Nick Jan 30 '12 at 14:07
1
class ContactMailer < ActionMailer::Base 
  default :to => Contact.all.map(&:contact_email),
          :from => "notification@example.com"

  def contact_email(user)
    recipients = user.contacts.collect(&:contact_email).join(',')
    mail(:subject => "Post added", :to => recipients)
  end
end
Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48
  • Hi Sandip - thanks for the answer, but with this code I still get the error `undefined method 'contacts' for #` - should I be declaring contacts in here too? – Nick Jan 30 '12 at 14:05
  • don't you have user has_many :contacts association defined inside user model ?? – Sandip Ransing Jan 30 '12 at 21:22