Active Storage. This tutorial includes the actual code you can use to try out this feature.
Active Storage is a built-in gem included on Rails 5.2 that handles file uploads to storage services from Amazon, Google, and Microsoft. Saving to local disk is also possible for development and test environments.
This gem replaces Paperclip, Carrierwave, Dragonfly.
We'll begin by creating a new application.
# install the rails 5.2 beta gem # gem install rails -v 5.2.0.beta2 rails new storageapp
A migration is also created automatically for new applications. If you want to copy the migrations manually, run
bin/rails active_storage:install:migrations bin/rails db:migrate
This migration adds two tables -
A Blob can be attached to one or more Active Record objects through the Attachment join model.
Active Record Model
Let's say you want to attach one or multiple images to a Comment. First, we have to create the Comment model. We'll use the generator to create the resource.
rails g resource comment content:text
You don't need to use the Blob and Attachment directly. To attach one image to a Comment, use
class Comment < ApplicationRecord has_one_attached :image end
You don't have to create an Image model. Active Storage uses the Blob and Attachment under the hood to give
Let's take a look at the controller and views.
class CommentController < ApplicationController def new comment = Comment.new end def create comment = Comment.create! params.require(:comment).permit(:content) comment.image.attach(params[:comment][:image]) redirect_to comment end def show comment = Comment.find(paramd[:id]) end end
# new.html.erb <%= form_with model: @comment, local: true do |form| %> <%= form.text_area :content %><br><br> <%= form.file_field :image, %><br> <%= form.submit %> <% end %>
# show.html.erb <%= image_tag @comment.image %>
This is typical code for creating an object. The Active Storage part
To display the image, we
We only need to modify a few things from the code above to use multiple attachments.
file_fieldto allow selection of multiple files
class Comment < ApplicationRecord has_many_attached :image end
class CommentController < ApplicationController def new comment = Comment.new end def create comment = Comment.create! params.require(:comment).permit(:content) comment.images.attach(params[:comment][:images]) redirect_to comment end def show comment = Comment.find(paramd[:id]) end end
# new.html.erb <%= form_with model: @comment, local: true do |form| %> <%= form.text_area :content %><br><br> <%= form.file_field :images, multiple: true %><br> <%= form.submit %> <% end %>
# show.html.erb <% @comment.images.each do |image| %> <%= image_tag image %> <br /> <% end %>
Active Storage is enabled by default when you create a new Rails application.
config/active_storage.yml is created
local is set to use Disk and
local: service: Disk root: <%= Rails.root.join("storage") %>
To change this, you can specify
For Amazon S3, you can
You also need to add the necessary gem on the Gemfile for your chosen service. This can
Active Storage makes it easy to handle file uploads to the storage services. We only talked about the basics here. In a future post, we'll talk about advanced features like direct uploads and variants.