smallroomsoftware.com

Reuse existing partials in ActionMailer HTML emails

Posted on November 18, 2007

This might be useful for anyone wanting to reuse action/controller templates/partials in HTML emails sent with ActionMailer. It causes templates/partials to be resolved relative to the 'views' directory rather than relative to the relevant mailer templates directory (which is the default).

module ActionMailer
  class Base
    # We have to go root relative here because of the change below
    def render_message(method_name, body)
      render :file => "#{mailer_name}/#{method_name}", :body => body
    end

    # This allows root relative partials to be located correctly
    def initialize_template_class(assigns)
      ActionView::Base.new(template_root, assigns, self)
    end
  end
end
Comments
  1. jakeDecember 07, 2007 @ 05:53 PM
    where does this go? I created a class and copied your code into /lib but am still unable to reuse existing partials in Action Mailer. Any help would be appreciated.
  2. tomJanuary 02, 2008 @ 03:34 PM
    It just needs to be in a file that's required during rails initialisation. So you could copy the code into your environment.rb or, better (with rails 2), put it in a file in the config/initializers/
Post a comment
Comment



If you can read this, you don't use a typical webbrowser that plays nice with CSS.
Please do not fill in anything here!



Hosting by site5.com