glebm / rails_email_preview

Preview and edit app mailer templates in Rails.

Home Page:http://glebm.github.io/rails_email_preview

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use instance variables?

barnett opened this issue · comments

I am trying to use an instance variable (such as an object from the db), within the content snippet yet am unsure how to proceed? (since it seems only helpers are given for content from the README) When I define them within the email action and just try to call within the view page I get an error.

You can define helper methods in helpers.

# app/helpers/emails_cms_helper.rb
module EmailsCmsHelper
  def greeting
    t('email.greeting', name: @user.name)
  end
end
class MyMailer ...
  helper :emails_cms

Use like so:

{{cms:helper:greeting}}
You have one new message

I've updated the wiki with this example.

I seem to still have a problem, am getting the error:

undefined method `f_name' for nil:NilClass

It is showing the following line as the error:

<%= greeting() %>

The helper looks like this:

module EmailsCmsHelper
  def greeting
    t('email.greeting', name: @donor.f_name)
  end
end

The mailer looks like this:

class Notifier < ActionMailer::Base
  helper :emails_cms

  default from: "from@example.com"

  def initial_donation_email(donor)
    mail(
      subject: cms_email_subject(name: donor.f_name),
      to: donor.email
    )
    @donor = donor
  end
  ...
end

It seems the @donor instance isn't accessible to the helper but unsure how I can fix it?

@blklane Try putting @donor = donor before the mail(...) call

@glebm yup, that does it, thank you!