When a teacher drops an image into an Action Text Trix editor, how is the file uploaded, referenced in the HTML and rendered later?
answer
- trix-attachment-add starts the upload
- Active Storage direct upload, not the form
- action-text-attachment tag with an sgid
- embeds set before validation
- active_storage/blobs/_blob partial
basics
~20 sAction Text's JavaScript direct-uploads the file to Active Storage when it is dropped, and the body stores an action-text-attachment tag with the blob's signed global id. Saving attaches the blob as an embed; rendering draws the blob partial.
solid answer
~40 sTrix fires `trix-attachment-add`; Action Text's JavaScript creates an Active Storage `DirectUpload` to the URL in the editor's `data-direct-upload-url`, which creates a blob and uploads the bytes straight to the storage service. The response's `attachable_sgid` and a URL built from `data-blob-url-template` are set on the Trix attachment; when the body is assigned on the server, Action Text converts Trix's attachment markup into an `<action-text-attachment sgid="..." content-type="image/png" ...>` element. When the lesson saves, `ActionText::RichText` runs a `before_validation` callback (before 8.1 it was `before_save`) that sets `embeds` to the blobs referenced in the body, attaching them through `has_many_attached :embeds`. At render time each tag's sgid is resolved to the blob and `app/views/active_storage/blobs/_blob.html.erb` draws it, an `image_tag` of a representation resized to 1024x768 (800x600 in galleries). Images removed before saving stay as unattached blobs.
code
erb · 9 lines<%# app/views/active_storage/blobs/_blob.html.erb, copied by action_text:install %>
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %>">
<% if blob.representable? %>
<%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %>
<% end %>
<figcaption class="attachment__caption">
<%= blob.try(:caption) || blob.filename %>
</figcaption>
</figure>go deeper
Recall that images in the editor are stored with Active Storage and that the content keeps a reference tag, not the file.
Walk through the flow: trix-attachment-add, direct upload, attachable_sgid in an action-text-attachment tag, embeds assigned on save, and the blob partial at render time.
Cover the operational side: orphaned uploads, server-side validation of embeds, image processing dependencies and how URLs and delivery mode apply to embedded files.
Judge whether direct-to-storage uploads from a rich-text editor fit your content policy, moderation and retention needs.
## Upload: before the form is submitted Action Text does not send embedded files with the form. When a teacher drops a photo of a score into the editor: 1. Trix creates an attachment and dispatches **`trix-attachment-add`**. 2. Action Text's JavaScript (`@rails/actiontext`) listens for it and, if the attachment holds a `File`, starts an `AttachmentUpload`, a wrapper around Active Storage's **`DirectUpload`**. 3. The upload POSTs the file's metadata to the URL in the editor's `data-direct-upload-url` (Rails' direct-uploads endpoint), which creates an `ActiveStorage::Blob`, and then sends the bytes directly to the storage service. 4. The JSON response includes the blob's **`attachable_sgid`**. Action Text sets it on the Trix attachment together with a `url` built from `data-blob-url-template` (a `rails_service_blob_url` with `:signed_id` and `:filename` placeholders), so Trix can show the image. 5. Progress is reported through `direct-upload:start`, `direct-upload:progress`, `direct-upload:error` and `direct-upload:end` events on the editor, each with `event.detail.attachment`; since Rails 8.1 the progress bar also accounts for server processing time. The editor serializes the attachment as a `<figure data-trix-attachment="...">` in the hidden input. When the form's HTML is assigned to `notes`, `ActionText::Content` canonicalizes it, converting every `[data-trix-attachment]` node into Action Text's own element, so the stored body holds HTML like: ```html <action-text-attachment sgid="BAh7CEkiCG..." content-type="image/png" url="https://lessons.example/rails/active_storage/blobs/redirect/.../etude.png" filename="etude.png" filesize="184211" width="1200" height="900" previewable="true"> </action-text-attachment> ``` ## Save: linking the blobs to the rich text The form submits only that HTML. `ActionText::RichText` declares `has_many_attached :embeds` and a callback: ```ruby before_validation do self.embeds = body.attachables.grep(ActiveStorage::Blob).uniq if body.present? end ``` `body.attachables` resolves every `<action-text-attachment>` sgid to its record; the blobs among them become the `embeds` collection. Assigning a `has_many_attached` replaces the set, so an image deleted from the content stops being an embed on the next save and, under the default `dependent: :purge_later`, its blob is purged in the background. Rails 8.1 moved this assignment from `before_save` to `before_validation`, so validations can already see the embeds. ## Render: from tag to markup When `<%= @lesson.notes %>` renders: - each `<action-text-attachment>` is resolved with its sgid (`ActionText::Attachable.from_node`, which looks the record up through Signed GlobalID); - a blob is rendered with **`active_storage/blobs/_blob.html.erb`**, the partial the installer copied into the app: a `<figure>` with `image_tag blob.representation(resize_to_limit: [1024, 768])` (`[800, 600]` inside a gallery) and a caption or the filename and size; - images that Trix placed together in a gallery are wrapped by the `action_text/attachment_galleries/_attachment_gallery` partial; - an sgid that no longer resolves renders `action_text/attachables/missing_attachable`. The resulting HTML is then sanitized. The embedded image URLs are ordinary Active Storage URLs, delivered in redirect or proxy mode like any other attachment. ## Consequences worth knowing | Situation | What happens | |---|---| | Image dropped, lesson never saved | blob exists, never attached; clean up with an `ActiveStorage::Blob.unattached` sweep | | Image removed from the content and saved | blob detached from `embeds` and purged later | | Lesson destroyed | rich-text row destroyed, embeds purged later | | `libvips` missing | uploads work, but the representation in `_blob` cannot be generated | | Non-image file (PDF) | stored the same way; the partial shows a preview only if a previewer accepts it | To restrict what teachers can embed, cancel `trix-file-accept` in JavaScript for unwanted types (a convenience), and validate the blobs in `embeds` on the server, since the upload endpoint itself accepts any file.
- How would you stop teachers from embedding files other than images?In the browser, listen for `trix-file-accept` and call `preventDefault()` for non-image types so Trix never starts the upload. On the server, add a validation on the rich text or the lesson that checks the `content_type` of each blob in `notes.embeds`, since the direct-upload endpoint accepts any file and a crafted request can skip the JavaScript.
- Why does the stored HTML keep working when the image's storage service changes?The content stores the blob's signed global id, not a storage URL. At render time Action Text resolves the sgid to the blob and the blob partial builds a fresh Active Storage URL, so a service change or new URL settings apply to old lessons automatically; the `url` attribute stored in the tag is not used while the sgid still resolves.
An embedded image works like a library call slip tucked into a manuscript: the text keeps only the slip's catalogue number, the book itself lives on the shelves, and the librarian fetches it whenever someone reads the page.
- Images in a rich_textarea are sent as multipart parts with the form submit
- The stored HTML contains a permanent storage URL that Action Text serves as is
- Embedded images are base64-encoded inside the body column
- Removing an image from the content leaves it attached as an embed forever
- Action Text renders embedded images without needing Active Storage
Assumes Rails 8.1: ActionText::RichText assigns embeds in before_validation (it was before_save until 8.0), and upload progress now includes server processing time.