Action Text

Rails’ built-in framework for handling rich-text content, allowing users to create, edit, and display formatted text with embedded images and attachments using the Trix editor.

Table of Contents

What Is Action Text?

Action Text is a Rails framework, introduced in Rails 6, that brings rich-text editing capabilities to your application. It integrates the open-source Trix editor, built by Basecamp, directly into Rails, giving users a WYSIWYG editing experience without needing to set up a third-party library.

Action Text handles everything from storing formatted content to embedding images and file attachments, all tied into Rails’ existing Active Storage and Active Record systems.

Why Is Action Text Useful?

Without Action Text, adding rich-text editing to a Rails app requires manually wiring up a third-party editor (like Quill or TipTap), handling content storage, sanitizing HTML, and managing file uploads separately. This is repetitive and error-prone.

Action Text solves this by providing:

  • A ready-to-use Trix editor rendered with a single helper

  • Automatic storage of rich-text content in its own database table, keeping your model tables clean

  • Built-in integration with Active Storage for handling embedded image and file uploads

  • Safe HTML rendering through a built-in content sanitizer, preventing XSS vulnerabilities

  • Full support for associations, any model can has_rich_text in just one line

How Does Action Text Work?

When you install Action Text, it creates a dedicated action_text_rich_texts table that stores all rich-text content as JSON-like HTML. This keeps your model’s own table lightweight — it simply holds a reference rather than a large text blob.

Key concepts:

has_rich_text – Declares a rich-text attribute on a model. Behind the scenes, this sets up a polymorphic association to the action_text_rich_texts table.

Trix Editor – The browser-side WYSIWYG editor that Action Text ships with. It handles formatting (bold, italic, lists, links), embedded attachments, and produces structured HTML output.

Active Storage Integration – File and image attachments made inside the Trix editor are automatically handled by Active Storage, using whatever storage backend is configured (local disk, S3, GCS, etc.).

Content Sanitization – Action Text sanitizes all rich-text output before rendering, using an allowlist of safe HTML tags, preventing scripts or malicious markup from being stored or displayed.

Rendering– In views, rich-text content is rendered using the standard <%= @model.content %> output, which Action Text converts to safe, formatted HTML automatically.

Examples

Scenario 1: Installing Action Text

bin/rails action_text:install 
bin/rails db:migrate 

This installs the required JavaScript package (Trix), creates the action_text_rich_texts table, and sets up Active Storage if not already configured.

Scenario 2: Adding Rich Text to a Model

# app/models/post.rb 
class Post < ApplicationRecord 
  has_rich_text :content 
end 

This single line gives the Post model a content attribute that stores and retrieves rich-text data.

Scenario 3: Using the Trix Editor in a Form

<%# app/views/posts/_form.html.erb %> 
<%= form_with model: @post do |f| %> 
  <%= f.label :content, "Body" %> 
  <%= f.rich_text_area :content %> 
  <%= f.submit %> 
<% end %> 

The rich_text_area helper renders the fully functional Trix editor in the browser.

Scenario 4: Rendering Rich-Text Content in a View

<%# app/views/posts/show.html.erb %> 
<%= @post.content %> 

Action Text automatically renders the stored content as sanitized HTML — no additional setup needed.

Scenario 5: Permitting the Attribute in the Controller

# app/controllers/posts_controller.rb 
def post_params 
  params.require(:post).permit(:title, :content) 
end 

Rich-text attributes are permitted just like any other model attribute through Strong Parameters.

Where Is Action Text Used?

  • Blog platforms and CMS-style Rails apps where users write and format long-form content

  • Project management tools needing formatted descriptions or comments (e.g., task bodies, notes)

  • Support or ticketing systems where agents write detailed, formatted replies

  • Documentation or wiki features built inside a Rails monolith

  • Any form field that needs to go beyond plain text — headings, bullet lists, inline images, file attachments

In Summary

Action Text is Rails’ built-in solution for rich-text editing. By combining the Trix editor, Active Storage for attachments, and a dedicated database table, it lets you add a full WYSIWYG editing experience to any model with minimal setup.