Problem with instance vars in explicit multipart email using ActionMailer

Hi everyone,

I’m having trouble accessing instance vars passed to my email template
as part of an explicit multipart message, and I wonder if anyone here
could help me.

The task at hand is to deliver email with only the text/html content
type if the message lacks a plaintext (from a record in the db) and to
send multipart email if it has both plaintext & html available.

Here’s the simplified code from the mailer:


def email(recipient, content)
    subject content.title
    from '[email protected]'
    headers 'return-path' => '[email protected]'

    if context.plaintext.empty? # force HTML email if the module

lacks plaintext
content_type ‘text/html’
body :content => content, :user => recipient
else # send a multi-part email
part “text/plain” do |p|
p.body = render_message(“email_plain”, :content =>
content, :user => recipient)
p.transfer_encoding = “base64”
part “text/html” do |p|
p.body = render_message(“email”, :content =>
content, :user => recipient)

This works fine if content.plaintext is empty – it properly delivers
the HTML-only email. If however, content.plaintext is not empty (ie,
it should send a multipart message), the template throws an error.
For a template like:


<%= @content.title %>

The error is:

NoMethodError (You have a nil object when you didn’t expect it! The
error occurred while evaluating nil.title)

Thank you for your help in looking at this. I’m much obliged for any
advice you can offer.