By: Vikas Bomenwar Category: Ruby on Rails Technologies: Ruby on Rails
Rails 6.0 comes with many new features and many improvements that can be beneficial for all small and bigger applications.
There are many new features introduced in rails 6.0, I have pickup up some of them to introduce in my blog.
In my blog I am going to introduce you with the important and cool gem which will be very useful for your application:
1. gem “action_text”
2. gem “active_storage”
1) Action Text
Action Text is a brand new feature introduced in Rails 6.0. It will be going to make creating, editing and displaying rich text content very easy in your application. It includes the Trix editor that handles everything from formating to list, to embed ed images.
The rich text content generated by the Trix editor using gem “action_text”is saved in its own RichText model that's associated with any existing Active Record model in the application. Any embedded attachments are automatically stored using Active Storage and associated with the included RichText model.
The Installation steps to use this feature in your application is given below:
Add the gem into Gemfile:
#
Gemfile
gem
"actiontext"
,
github:
"rails/actiontext"
,
require:
"action_text"
gem
"image_processing"
,
"~> 1.2"
Then run bundle install command
After installing the gem to Install assets, npm dependency, and migrations run below command:
rails action_text:install
The above command will generate the two migrations files for action text then you have to run below command:
rails db:migrate
add this to your view/layout/application in the head section
#layouts/application.html.erb
<%=
javascript_pack_tag
'application'
%>
In your model, it may be an article or whatever in your model
class
Article
<
ApplicationRecord
has_rich_text :content
end
In your controller, you have to permit the action text content
#controllers/articles_controller.rb
def
article_params
params.require(:article).permit(:name,
:content)
end
in your form
#_form.html.erb
<div
class
=
"field"
>
<%=
form.label :content %>
<%=
form.rich_text_area :content %>
</div>
finally to display the content in your view;
<h1><%=
@article
.name %></h1>
<p><%=
@article
.content %></p>
2. Active storage
Active Storage makes it simple to upload and reference your files in cloud services, like Amazon S3 or Google Cloud Storage, and attach those files to Active Records. It also provides a disk service for testing or local deployments, but the main focus is on cloud storage.
The main difference in how Active Storage works compared to other attachment solutions in Rails is through the use of built-in Blob and Attachment models (backed by Active Record). This means that existing application models do not need to be modified with additional columns to associate with files. Active Storage uses polymorphic associations via the join model of Attachment, which then connects to the actual Blob.
The Installation steps for Active storage are given below:
Add gem "activestorage", git: "https://github.com/rails/activestorage.git" to your Gemfile.
Add require "active_storage" to config/application.rb, after require "rails/all" line.
Run rails activestorage:install to create needed directories, migrations, and configuration.
Configure the storage service in config/environments/* with config.active_storage.service = :local that references the services configured in config/storage_services.yml.
Optional: Add gem "aws-sdk", "~> 2" to your Gemfile if you want to use AWS S3.
Optional: Add gem "google-cloud-storage", "~> 1.3" to your Gemfile if you want to use Google Cloud Storage.
How to use Active Storage it in your Application:
// in your model file message.rb
class
Message < ApplicationRecord
has_rich_text :content
end
// in your model file message.html.erb
<%= form_with
model: @message do |form| %>
<%= form.text_field :title, placeholder: "Title" %><br>
<%= form.text_area :content %><br><br>
<%= form.file_field :images, multiple: true %><br>
<%= form.submit %>
<% end %>