Rails 6.0.0 was released a couple of weeks ago, and despite spending my day-to-day working on a Rails 3 application, I like to torture myself occasionally with small ventures into the latest and greatest. I saw DHH post this article introducing Action Text and wanted to see it in... action
It was really easy to setup and start using and personally I think it's a great addition to Rails. But there is some discussion and debate about it, with some valid points on both sides, which I'll discuss further down.
But I'm very much aware there are two types of blog post reader! And for those who just want to see the code snippets so they can can crack on, here you go...
Step 1 – Create and setup your Rails 6 application
$ gem install rails -v 6.0.0
And create your application. (Note that I prefer Postgres, so have swapped out sqlite. But this is not a requirement.)
$ rails new action-text-demo-app -d postgresql
Now jump into the app.
$ cd action-text-demo-app
And create your database.
$ bin/rails db:create
Step 2 - Add Action Text
So we're going to be hooking into Active Storage (so that we can embed images in our text), so let's uncomment the gem and bundle install.
# Gemfile gem 'image_processing', '~> 1.2'
$ bundle install
And now we can install Action Text.
$ bin/rails action_text:install
You'll see this creates you a couple of migrations, so let's go ahead and run them.
$ bin/rails db:migrate
Step 3 – Implementation
So we're ready to go. We can pretend we're building a new app for collecting feedback on your colleagues.
We can use the Rails scaffold to create a
Feedback model with a few basic fields to capture this valuable and objective feedback!
$ bin/rails g scaffold Feedback colleague_name:string $ bin/rails db:migrate
Wait! Where are the valuable and objective input fields???
Remember the migrations that
action_text:install provided? Did you check them out?
So this is one of areas where Action Text divided opinion here at Kyan. We've now got a table
action_text_rich_texts which stores each text input using a polymorphic association.
- It's a nice way of keeping the model light
- Large text fields aren't being stored in memory when you're working with your instances
- You can add additional text fields without needing to write and run any more migrations
- The schema doesn't provide an accurate picture of our model
- Finding and debugging data in the database now involves an additional table
action_text_rich_textstable could grow quickly; not ideal when trying to keep row counts down (think free tier hobby DBs!) or could decrease performance
We've digressed from the code snippets, apologies! JUST TELL ME HOW TO USE ACTION TEXT!
So it's as simple as adding the following snippet to your model(s). Let's add three text fields.
# app/models/feedback.rb class Feedback < ApplicationRecord has_rich_text :attitude has_rich_text :coffee_making_skills has_rich_text :general_opinion end
And then add the fields to our
_form.html.erb (you'll obviously need to do this for each field you've added). You'll see we're using a new form helper
# app/views/feedbacks/_form.html.erb <%= form.label :attitude %> <%= form.rich_text_area :attitude %>
Don't forget to add the fields to your
show.html.erb but more importantly... don't forget to permit these fields in your controller!
And of course, we need to add the routes.
# config/routes.rb Rails.application.routes.draw do resources :feedbacks end
And there you go! Navigate to your new Feedback form (
/feedbacks/new) and you'll be able to start using Action Text!
Even more can be found in the Github repo which adds that "Trix supports all evergreen, self-updating desktop and mobile browsers."
So why am I fan of Action Text? And why are others not so keen?
For me, a developer still new in this world, the appeal of a framework like Ruby on Rails is still a huge draw. And Action Text, like Active Storage, is another out-the-box feature that makes prototyping and side projects come to life so quickly.
The combination of Action Text and Active Storage allow embedded images within the text field too. With such little effort!
But, you can't be all things to everyone. Action Text (or Trix) does not support tables, nor does it have mutliple header sizes. You are limited to the feature set that Trix have provided. And for a production application this may not be enough.
The process of separating the input into a
action_text_rich_texts table also means that replacing it later down the line is likely to require a bit more effort – similarly migrating to it during an upgrade. Whilst Action Text could be an amazing addition for even speedier MVP development, I'm not sure how easy it would be down the line to replace it.
My impression, is that this would have been an extremely welcome feature in Rails 3 or 4 when more CMS websites were being built. Maybe it's a little too late for it to be lauded in the same way as other new Rails 6 features like multiple database support and parallel testing, but I still think it's a welcome addition and will come in great for the many side projects I start... and inevitably ditch!