Search code examples
ruby-on-railsrails-activestorageactiontext

ActionText blob partial not rendering extra elements


I am using ActionText to render some richtext and for some reason I have restrictions in adding extra elements to the rendered partial using the partial provided by actiontext

I require attached images to be wrapped around a table element, so they can be rendered correctly for emails in Outlook. ActionText seems to always ignore any extra elements in this partial I put in. Anyone know why this is happening and if any ways to work around this?

This is my partial, where the table elements get ignored:

/views/active_storage/blobs/_blob.html.erb

<table>
  <Tr>
    <td>
      <figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %> text-center">
        <% if blob.representable? %>
          <%= image_tag rails_representation_url(blob.representation(resize_to_limit: [600, 400])) %>

          <% if caption = blob.try(:caption) %>
            <figcaption class="attachment__caption text-center">
              <%= caption %>
            </figcaption>
          <% end %>
        <% end %>
      </figure>
    </td>
  </Tr>
</table>

Solution

  • I found the issue was to do with Action Text using inner method for sanitising strings before render.

    I override the allowed attributes and tags in a initialiser:

    ActionText::ContentHelper.allowed_attributes.add 'style'
    ActionText::ContentHelper.allowed_attributes.add 'controls'
    
    ActionText::ContentHelper.allowed_tags.add 'video'
    ActionText::ContentHelper.allowed_tags.add 'source'