Upgrading to Rails 6 and getting started with Action Text

With Rails 6 coming out soon, many developers will have the opportunity to upgrade their applications and use some brand new, shiny features.

As of a few days ago, the first beta version of Rails 6, 6.0.0.beta1, was released. Here is a guide on how to upgrade your existing Rails application to it, and getting started with one of the new core feature, Action Text.

Upgrading your Rails application

Before you jump in

As with any Rails upgrade, you want to make sure that your current code is stable, and well tested. A major upgrade comes with the possibility of breaking changes, and Rails 6 is no exception.

I am also assuming that you are already on the latest stable version of Rails, 5.2.x.

Make sure your Ruby version is compatible

The first change that comes with Rails 6 is the minimum Ruby version it requires. While Rails 5 required at least Ruby 2.2.2, Rails 6 will make you upgrade to Ruby 2.5.0 or higher.

If you are using Homebrew and rbenv, you can easily do so by running the following commands:

brew update && brew upgrade ruby-build
rbenv install 2.6.0

Edit your Gemfile

As the version you will install is still in beta, you will need to manually points to the right commit.

gem "rails", "6.0.0.beta1", github: "rails/rails", ref: "5a0230c67f852fb21173f99647e76336897446c1"

You should now be good to go. To install the new dependencies, run:

bundle update rails

Use the app:update task to configure your application

The included app:update task will come in handy for this upgrade, as it will add Action Text and Action Mailbox as required .

rails app:update

Setting up Action Text

Action Text works in a similar way as Active Storage and will have its own database table with polymorphic associations.

This means that you do not need to add migrations for your existing models, and that you will be able to use Action Text right out of the box.

Installing Action Text

In order to get the Action Text features, you will need to run one more task.

rails action_text:install

Running this command will create several files, including a default stylesheet and add the Trix editor as a dependency via yarn.

It will also generate a new database migration file to create the table needed for Action Text support.

Add rich text support to your models

Adding rich text support to any of your models is really straight-forward. Simply edit the model and use has_rich_text as shown below. Forms come with a new helper to create a fancy text area with text editing options.

Model

# app/models/post.rb
class Post < ApplicationRecord
has_rich_text :content
end

View

# app/views/posts/new.html.erb
<%= form_with(model: @post, local: true) do |form| %>
<%= form.label :content %>
<%= form.rich_text_area :content %>
<%= form.submit "Save" %>
<% end %>

Conclusion

Ruby on Rails’ convention over configuration strikes once again, and provides a very easy way to offer rich text editing to users.

Hopefully this article helps you in upgrading to Rails 6 and getting started with Action Text.

Let me know how your initial experience with the new Rails feature was on Twitter.

References