Rails 5.2 is released with RC1 Status which is considerably stable.
To install rails 5.2 you will have to do gem install rails — prerelease.
Rails 5.2 is the major update in rails before the release of Rails 6. In this 5.2 release, most talked about feature is Active Storage. Active Storage is built in functionality for handling file uploads without using any external gem like ‘paperclip’, ‘carrierwave’ or any other. According to DHH, Active Storage is extracted from Base Camp 3. “So it claims to be framework born from the production.”
In this article, we are going to talk about Rails 5.2’s most talked about feature which is Active Storage
With attached things
We are not going to compare Active Storage with any other solution for uploading files.
To enable Active Storage in your application, we will have to run
rake task rails active_storage:install
The above command will add a new migration file in the folder db/migration. Once executed, it creates two tables that Active Storage needs to deliver its promises: active_storage_attachments and active_storage_blobs.
“Active Storage uses polymorphic associations via the Attachment join model, which then connects to the actual Blob. Blob models store attachment metadata (filename, content-type, etc.), and their identifier key in the storage service.”
Active storage also allows you to choose where to keep your files, be it your hardware, Amazon S3, Google Cloud Storage, Microsoft Azure are supported out of the box.
Guessing in wholeheartedness of learning about Active Storage you have already created the new rails 5.2 app. Now comment out the JBuilder in your Gemfile and do bundle install.
Now we will generate scaffold for Post, Which will create all the necessary files.
$ rails g scaffold post title:string content:text
The Above command will generate a Post Model, PostController and related view files.
To extend the features of Active Storage we will run the rake task as earlier said.
rake task rails active_storage:install
Now run the migration.
Let’s start by attaching a single image to each post.
So inside your post.rb file
class Post < ApplicationRecord
To get started with Active Storage and attaching files to our post. We will have to make 3 changes. We already have taken care of the first.
For the second one. We will add an attached file option in our form. In the form partial inside views/posts.
We will add file_field in the file, which ruby illustrates as file type input in UI
<%= form.label :image %>
<%= form.file_field :image %>
By adding the above snippet now we will be able to browse files and attach it to the form instance.
When the above form is submitted, all the form data will go to post controller.
We will have to make some changes in order to accept the image, which is our third and last change.
In PostController post_params add image attribute so we can accept it.
If we want to attach image inside the existing model anywhere in the controller we can do it by:
Note: If you are using create or update a resource in your controller action and pass attachment as a permitted parameter, the line above is not needed and it’ll break things. Most of the tutorials prefer to attach a file explicitly, but that is not the case anymore.
Now in order to show the attached image, we need to do add the following
<% if @post.image.attached? %>
<%=image_tag @post.image %>
We are using .attached? To check image is attached to that post or not, because of @post.image.present? Will always be true.
Now the application with Active Storage functionality is ready for testing.
For local we can run the server by rails s or rails s -p 4000 (your desired free port).
Now test the functionality we have just built.
Editing this post and changing an image will work right away too, just give it a try. And you can see file uploads are enabled in your application without using any gem or external support.
A quick recall of what we did
Model: We specified has_one_attached method in the model definition with a symbol associated with it, that will act as a virtual attribute on each instance of our model. We prefer the name :image for it, but it could have been :any_name_you_want.
Controller: We whitelisted image as a permitted parameter.
Views: We added afile_field to our form and displayed an uploaded image in the image_tag.
To read about it in more detail you can check it the GitHub repo