DEV Community

Cover image for ActionText, bringing rich text content management to Rails Core
fedeagripa
fedeagripa

Posted on

ActionText, bringing rich text content management to Rails Core

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

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

How do I implement it?

code

# Use ActiveStorage variant
gem 'image_processing'
Enter fullscreen mode Exit fullscreen mode

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:

class Post < ApplicationRecord
  has_rich_text :content
end
Enter fullscreen mode Exit fullscreen mode

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

def post_params
  params.require(:post).permit(:title, :content)
end
Enter fullscreen mode Exit fullscreen mode

Your view

<div class="field"></div>
Enter fullscreen mode Exit fullscreen mode

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

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

The solution provided up to now to solve this is to make your API accept attachments markup in canonicalized form (e.g. ) instead of the full Trix-compatible markup (like above). To achieve this you would need Basecamp Doc

Conclusion

magic
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

Top comments (5)

Collapse
 
thatosmk profile image
Thato Semoko

Before Rails 6 i remember having to find a good editor, Summernote rails gem was not to bad but still wasn't good enough for what I wanted to achieve. ActionText is far superioir and Trix editor is great.

Collapse
 
fedeagripa profile image
fedeagripa

Yeah definitely is a great option! I find it still a bit un mature for just backend development, but nothing that a couple of hours of open source can't solve !

Collapse
 
itsjzt profile image
Saurabh Sharma

you use linux?

Collapse
 
fedeagripa profile image
fedeagripa

Nop, OSX

Collapse
 
itsjzt profile image
Saurabh Sharma

This penguin reminds of of that😆