You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/action_text_overview.md
+50-25Lines changed: 50 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,10 +21,13 @@ Action Text brings rich text content and editing to Rails. It includes
21
21
the [Trix editor](https://trix-editor.org) that handles everything from formatting
22
22
to links to quotes to lists to embedded images and galleries.
23
23
The rich text content generated by the Trix editor is saved in its own
24
-
RichText model that's associated with any existing Active Record model in the application.
24
+
RichText model that can be associated with any existing Active Record model in the application.
25
25
Any embedded images (or other attachments) are automatically stored using
26
26
Active Storage and associated with the included RichText model.
27
27
28
+
When it's time to render the content, Action Text processes it by sanitizing its
29
+
content so that it's safe to embed directly into the page's HTML.
30
+
28
31
## Trix Compared to Other Rich Text Editors
29
32
30
33
Most WYSIWYG editors are wrappers around HTML’s `contenteditable` and `execCommand` APIs,
@@ -80,7 +83,7 @@ $ bin/rails generate model Message content:rich_text
80
83
81
84
NOTE: you don't need to add a `content` field to your `messages` table.
82
85
83
-
Then use [`rich_text_area`] to refer to this field in the form for the model:
86
+
Then use [`rich_text_area`][] to refer to this field in the form for the model:
84
87
85
88
```erb
86
89
<%# app/views/messages/_form.html.erb %>
@@ -92,16 +95,6 @@ Then use [`rich_text_area`] to refer to this field in the form for the model:
92
95
<% end %>
93
96
```
94
97
95
-
And finally, display the sanitized rich text on a page:
96
-
97
-
```erb
98
-
<%= @message.content %>
99
-
```
100
-
101
-
NOTE: If there's an attached resource within `content` field, it might not show properly unless you
102
-
have *libvips/libvips42*package installed locally on your machine.
103
-
Check their [install docs](https://www.libvips.org/install.html) on how to get it.
104
-
105
98
To accept the rich text content, all you have to do is permit the referenced attribute:
106
99
107
100
```ruby
@@ -117,28 +110,60 @@ end
117
110
118
111
## Rendering Rich Text Content
119
112
120
-
By default, Action Text will render rich text content inside an element with the
121
-
`.trix-content` class:
113
+
Instances of `ActionText::RichText` are capable of safely rendering themselves, so embed them directly into the page:
114
+
115
+
```erb
116
+
<%= @message.content %>
117
+
```
118
+
119
+
Learn more about Action Text's sanitization processin the documentation for the [`ActionText::RichText`](https://api.rubyonrails.org/classes/ActionText/RichText.html) class.
120
+
121
+
NOTE: If there's an attached resource within `content` field, it might not show properly unless you
122
+
have *libvips/libvips42* package installed locally on your machine.
123
+
Check their [install docs](https://www.libvips.org/install.html) on how to get it.
124
+
125
+
By default, Action Text will render rich text content inside an element with the `.trix-content` class. Elements with this class, as well as the Action Text editor, are styled by the [`trix` stylesheet](https://unpkg.com/trix/dist/trix.css). To provide your own styles instead, remove `trix` from the pre-processor directives in the `app/assets/stylesheets/actiontext.css` file created by the installer:
126
+
127
+
```diff
128
+
/*
129
+
* Provides a drop-in pointer for the default Trix stylesheet that will format the toolbar and
130
+
* the trix-editor content (whether displayed or under editing). Feel free to incorporate this
131
+
* inclusion directly in any other asset bundle and remove this file.
132
+
*
133
+
- *= require trix
134
+
*/
135
+
```
136
+
137
+
To customize the HTML container element that's rendered around rich text content, edit the `app/views/layouts/action_text/contents/_content.html.erb` layout file created by the installer:
To provide your own styles instead, remove the `= require trix` line from the
133
-
`app/assets/stylesheets/actiontext.css` stylesheet created by the installer.
147
+
To customize the HTML rendered for embedded images and other attachments (known as blobs), edit the `app/views/active_storage/blobs/_blob.html.erb` template created by the installer:
134
148
135
-
To customize the HTML rendered around rich text content, edit the
136
-
`app/views/layouts/action_text/contents/_content.html.erb` layout created by the
137
-
installer.
138
-
139
-
To customize the HTML rendered for embedded images and other attachments (known
140
-
as blobs), edit the `app/views/active_storage/blobs/_blob.html.erb` template
This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.
790
790
791
+
Both Action View and Action Text build their [sanitization helpers](https://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html) on top of the [rails-html-sanitizer](https://github.com/rails/rails-html-sanitizer) gem.
792
+
791
793
As a second step, _it is good practice to escape all output of the application_, especially when re-displaying user input, which hasn't been input-filtered (as in the search form example earlier on). _Use `html_escape()` (or its alias `h()`) method_ to replace the HTML input characters `&`, `"`, `<`, and `>` by their uninterpreted representations in HTML (`&`, `"`, `<`, and `>`).
0 commit comments