Using Action Text in a Rails 5.2 Application

Episode #167 by Teacher's Avatar David Kimura

Summary

With Rails 6.0 is around the corner, Action Text has been merged into the Rails repository. However, it can still be used on a Rails 5.2 application while we wait for its official release.
rails view wysiwyg 7:28

Resources

Summary

# Terminal
rails new template --webpack
rails action_text:install
rails g scaffold article new
rails db:migrate
brew install imagemagick vips

# Gemfile
# gem 'actiontext', github: 'rails/actiontext', branch: 'archive', require: 'action_text'
gem 'actiontext', github: 'kobaltz/actiontext', branch: 'archive', require: 'action_text'
gem 'image_processing'

# models/article.rb
class Article < ApplicationRecord
  has_rich_text :content
end

# controllers/articles_controller.rb
def article_params
  params.require(:article).permit(:name, :content)
end

# _form.html.erb
<div class="field">
  <%= form.label :content %>
  <%= form.rich_text_area :content %>
</div>

# layouts/application.html.erb
<%= javascript_pack_tag 'application' %>

# show.html.erb
<h1><%= @article.name %></h1>
<p><%= @article.content %></p>

# views/active_storage/blobs/_blob.html.erb
<%= image_tag blob.representation(resize: local_assigns[:in_gallery] ? "800x600" : "1024x768") %>