1

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
fydelio
  • 932
  • 1
  • 8
  • 22

1 Answers1

0

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

CWitty
  • 4,488
  • 3
  • 23
  • 40
  • Finally I solved it in the controller, using .to_s and then .gsub!. The liquid gem would have been an overkill for my purpose (but good tip). Though I did try to extend the content class, but more out of lacking experience in monkey patching. I opened the ActionText:Content class and add my custom method. The method was there, but when called on the action_text.body, I'd get a 'NoMethodError for "render"'. Somehow the 'include Render' doesn't work for added methods through monkey patches. Tips are apprechiated. – fydelio Dec 04 '20 at 10:13