Asherlc
Asherlc

Reputation: 1161

Undefined method "to_liquid" in mailing, but not in page

I'm getting an Liquid error: undefined method 'to_liquid' for #<Email:0x007f9ca1a62d28> error on my Email object when sending an email using a Liquid template inside a view. However, when I render the view by itself in the browser, it works just fine!

Here's my view:

= raw @template.render('email' => @email, 'organization' => @organization)

%p= sanitize("<a href='{{ unsubscribe_url }}''>Click here to unsubscribe</a>")

%p{style:'text-align:center;'}
  Sent with
  = link_to 'Vocalem', 'http://vocalem.com'

The relevant parts of my mailer:

class BulkMailer < ActionMailer::Base
  def bulk_email(recipients, email, organization)
    ...
    @organization = organization
    @email = email
    @template = Liquid::Template.parse(organization.current_email_template.body)

Relevant model code:

liquid_methods :subject, :body, :id

And last but not least, an example of the Liquid giving the error (but working fine in the plain view!):

{{email.body}}

Oddly, {{organization.display_name}} doesn't throw an error, but is just totally blank in the email (despite once again working fine in the plain view).

Any clue what might be going on here?

Upvotes: 2

Views: 2361

Answers (1)

Amar
Amar

Reputation: 6942

Try to create liquid methods using to_liquid in organisation model and check

def to_liquid
  {
   'display_name'=>self.name,
   'email_body'=>self.current_email_template.body 
  }
end

Upvotes: 6

Related Questions