Trix
field :body, as: :trix
The Trix
field renders a WYSIWYG Editor and can be associated with a string
or text
column in the database. The value stored in the database will be the editor's resulting HTML
content.
Trix field is hidden from the Index
view.
Options
-> always_show
Default
false
Possible values
true
, false
-> attachments_disabled
-> hide_attachment_filename
Default
false
Possible values
true
, false
-> hide_attachment_filesize
Default
false
Possible values
true
, false
-> hide_attachment_url
Default
false
Possible values
true
, false
-> attachment_key
Default
nil
Possible values
nil
, or a symbol representing the has_many_attachments
key on the model.
File attachments
WARNING
You must manually require activestorage
and image_processing
gems in your Gemfile
.
# Active Storage makes it simple to upload and reference files
gem "activestorage"
# High-level image processing wrapper for libvips and ImageMagick/GraphicsMagick
gem "image_processing"
Trix supports drag-and-drop file attachments. To enable Active Storage integration, you must add the attachment_key
option to your Trix field.
field :body, as: :trix, attachment_key: :trix_attachments
That attachment_key
has to have the same name as the model.
class Post < ApplicationRecord
has_many_attached :trix_attachments
end
Now, when you upload a file in the Trix field, Avo will create an Active Record attachment.
Disable attachments
You may want to use Trix only as a text editor and disable the attachments feature. Adding the attachments_disabled
option will hide the attachments button (paperclip icon).
field :body, as: :trix, attachments_disabled: true
Remove attachment attributes
By default, Trix will add some meta-data in the editor (filename, filesize, and URL) when adding an attachment. You might not need those to be present in the document. You can hide them using hide_attachment_filename
, hide_attachment_filesize
, and hide_attachment_url
.
Active Storage
Trix integrates seamlessly with Active Storage. When you use it with a plain database column on a record table (not with Action Text) you have to set the attachment_key
option (documented above).
Action Text
Trix integrates seamlessly with Action Text. It will automatically work with Action Text as well and it won't require you to add an attachment_key
.
Demo app
We prepared a demo to showcase Trix's abilities to work with Action Text and Active Storage.
Javascript Alert Messages
Since v3.13.8You can customize the javascript alert messages for various actions in the Trix editor. Below are the default messages that can be translated or modified:
avo:
this_field_has_attachments_disabled: This field has attachments disabled.
you_cant_upload_new_resource: You can't upload files into the Trix editor until you save the resource.
you_havent_set_attachment_key: You haven't set an `attachment_key` to this Trix field.
Refer to the default for more details.