سؤال

I have a number of mailers in my Rails 3.2 application, and I would like all of them to use the same email layout. That layout includes a header and footer, which includes images to be used inline. I'd like to follow the DRY principle of Rails, but I'm not entirely sure what the best practices are for this.

As this answer points out, I can use AbstractController::Callbacks to create a before_filter to add inline attachments. But how do I do this as well as set the layout in one module that I can then include in the mailers?

Ideas I have at the moment are to create a new mailer class called DefaultMailer, then have all mailers that I want to use the layout inherit from that class. Or to create a concern that would handle these tasks. Before attempting to hack something together using one of those techniques, I thought I would ask here to see if anyone had done this successfully before to help guide me.

هل كانت مفيدة؟

المحلول

AFAIK there are some issues when inheriting from other mailers (ie. not inheriting the settings).

Common solution is to create a mailer base module. This is a snippet from existing project:

module MailerBase
  extend ActiveSupport::Concern

  included do 
    helper :application
    layout 'mailer'
    default from: "#{AppConfig.application_name} <#{AppConfig.mailer_sender}>"
  end
end

You can combine this with your callbacks, although including remote images is more common and arguably better solution.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top