Home Using Action Text in Rails 6
Post
Cancel

Using Action Text in Rails 6

In order to have Action Text in your application you have to follow a few steps. We need to install Active Storage and then Action Text.

Install Action Text

1
2
# Terminal: Development
rails action_text:install

Import style and javascript

1
2
3
// application.js
require("trix")
require("@rails/actiontext")
1
2
3
// application.scss
@import "trix/dist/trix";
@import "./actiontext.scss";

Now we can attach it to a model, render a rich_text_area in our form, then render the content and we’re done

1
2
3
4
# post.rb
class Post < ApplicationRecord
  has_rich_text :content
end
1
2
3
4
5
# _form.html.erb
<%= form_with model: post do |form| %>
  <%= form.label :content %>
  <%= form.rich_text_area :content %>
<% end %>
1
2
# show.html.erb
<%= @post.content %>
This post is licensed under CC BY 4.0 by the author.