// you’re reading...

Coder()

Ruby on Rails - ARMailer - and View Templates

While working on Zategories.com it was ‘discovered’ that…

…the default ARmailer template feature doesn’t work the way it is documented.

At least if you want to use the features where you just have to put the email templates in a directory and the mailer generates and sends a multi-part email for ya.

Why?

Because this:

ActionMailer::Base.template_root = “mailer/templates”

does not work. Why, you might ask?

Because, in ActionMailer::Base, template_root is defined as:

class_inheritable_accessor :template_root

So assigning it a value like it was a class variable, doesn’t work because it is an instance variable.

What I did was to copy exception_notifier’s approach and override the accessor method in our notifier.rb model class:

def template_root
‘app/views’
end

Now, our notifier model class [subclassing ActionMailer::ARMailer, used to be ActionMailer::Base]
can use templates, like:

new_photo_notification.text.plain.rhtml
new_photo_notification.text.html.rhtml

which we put in a directory under apps/views/notifier

Discussion

Comments are disallowed for this post.

Comments are closed.

Categories