Use ActionText to create emails: Replace the placeholders like #LASTNAME with actual values from DB

432 Views Asked by At

I'm using rails ActionText to create emails for training invitations for all participants. These emails contain a custom attachment with reference to the training (like date / time of training, etc), but also some sort of salutation, like

Dear #first_name

When sending out these invitations as emails, I would like to replace the placeholder #first_name with the actual name from the database.

@training.action_text_invitation_text.body.to_s.gsub('#first_name', 'John')

The problem: The output get's converted to a String class and the contents of the custom attachment is no longer outputed (just the <action-text-attachment>, but with no content. The same happens for the methods to_html.

I've been thinking about extending the ActionText::Content class. But unsure how to do this?

module ActionText
  class Content
    def custom_render
     ... replace the certain keywords
    end
  end
end
1

There are 1 best solutions below

1
On

You could possibly use ActionText::Rendering.render. Alternatively you could use the liquid gem or something more purpose built.