ActionText, bringing rich text content management to Rails Core
Tags: rails, beginners, tutorial, programming
Published: 2020-06-10 20:31:26

ActionText is a new feature developed in Rails 6 that aims to solve managing rich text content on apps on an easy way following conventions. At first sight, this may seem like a trivial and insignificant feature, but you will see that inside of it you have some exciting things that may do your coding easier.

The purpose of this post is to introduce this new feature, explaining the specific case it was build for and the provide a personal opinion about the potential of it.

“Action Text brings rich text content and editing to Rails. It includes the Trix editor that handles everything from formatting to links to quotes to lists to embedded images and galleries. The rich text content generated by the Trix editor is saved in its own RichText model that’s associated with any existing Active Record model in the application. Any embedded images (or other attachments) are automatically stored using Active Storage and associated with the included RichText model.” [1]

# But… what actually is this all about?

The most straightforward implementation (and what it was first build for) is to manage content on web apps as you are used in Word (or Open Office) :

![Markdown editor](https://lh3.googleusercontent.com/proxy/2g1byzHLBjzZq0-LIL5mizU6YvuzFLcNbIbpNOoWzhVOx2WrQBnZMiEqQnAdgKJxgqKKhX7E9tHb5vf7TwTo02vMJNfiEEwG0VTDFwz7OGhUQAorZ3j2O5lMkA)

This may seem trivial at first sight, but trust me you will have a lot of headaches with the many cases you should consider and the time it will consume. With ActionText you will just spend 10 minutes.
![doubt](https://media.giphy.com/media/U24Ize3546YqEdlJ95/giphy.gif)

# How do I implement it?
![code](https://media.giphy.com/media/ule4vhcY1xEKQ/giphy.gif)

```ruby
# Use ActiveStorage variant
gem 'image_processing'
```
You need image_processing to deal with blobs automatically, this is another amazing thing provided here!. After running  bundle this will generate 3 new tables: action_text_rich_texts, active_storage_attachments, active_storage_blobs.

Your model:
```ruby
class Post < ApplicationRecord
  has_rich_text :content
end
```
After running migrations this will just generate a string column on your model, but don’t worry, it will use the previous tables too.

Your controller
```ruby
def post_params
  params.require(:post).permit(:title, :content)
end
```

Your view
```ruby
<div class="field"></div>
```
And that’s it, you have an app capable of storing chat messages, posts, images and any other rich text implementation you want with just a few lines.

# And… what happens if I’m just a backend developer?
![BE dev](https://i0.wp.com/www.rootstrap.com/blog/wp-content/uploads/2018/12/screen-shot-2018-12-06-at-11-04-16.png?w=352&ssl=1)

You will tend to think that there is just value in has_rich_text :content, because if you actually try to expose the content param your Front-end developer will have to generate something like this:
![data-attachment](https://i2.wp.com/www.rootstrap.com/blog/wp-content/uploads/2018/12/screen-shot-2018-12-05-at-16-08-55.png?w=669&ssl=1)

The solution provided up to now to solve this is to make your API accept attachments markup in canonicalized form  (e.g. <action-text-attachment sgid="…"></action-text-attachment>) instead of the full Trix-compatible markup (like above). To achieve this you would need Basecamp Doc

# Conclusion
![magic](https://media.giphy.com/media/12NUbkX6p4xOO4/giphy.gif)
This is a handy feature if you are building a full-stack Rails app, and for API development you would need to ask your Front-end developer to provide some specific information, anyway, it seems extremely useful as you are dealing with all content types automatically then.

Last but not less important, this is a feature under development yet so there will be some changes and improvements in the near future.

# References
[1] https://github.com/rails/actiontext