With Rails 5 we now have ActiveStorage: built in file management with the framework. This is a fantastic adition to the framework as it standardises file management, instead of relying on 3rd party plugins. ActiveStorage adds two tables to your app that handles all the file details (active_storage_blobs) and the actual attachments (active_storage_attachments). Once those are added to the schema, no more migrations are required as you develop your app, adding single or multiple attachments to your models.
By default, files are uploaded to the web server's file system. This is great when deployed at the developers workstation or for testing or when deployed to a single web server. For more complex sites or when deployed to platform-as-a-service systems like Heroku, it's very simple to configure ActiveStorage to upload to AWS S3, DigitalOcean Spaces or whatever other file hosting service you need to use. You can even set mirroring in the storage.yml file. ActiveStorage has been well thought out for even the most heavy load sites.
Once you have enabled ActiveStorage it's very simple and intuative to add attachments to a model. To add a single file per record, add a has_one_attached macro, eg:
Class User < ApplicationRecord has_one_attached :avatar end
To add multiple files per record, just add a has_many_attched macro
Class User < ApplicationRecord has_many_attached :images end # view file <% form.field_field :avatar %> <% form.file_field :images, multiple:true %>
This is amazing. An awful lot is going on behind the scenes, but as you develop your applications's functionality you don't have to worry about it. It just works straight away.
Baked in, you can manipulate image uploads.
Add the gem:
and then resize:
<%=image_tag user.avatar.variant(resize: "100x100") %>
There are even plugins for previewing other attachments as images such as Videos and PDFs. Handy.
Built right into ActiveStorage we have the ability to upload files directly to the file storage service - avoiding load and wait time on your webserver. This is an awesome feature that comes complete with file upload progress JS and CSS.
Thats it for active storage. What an awesome addition to RubyOnRails. The best web framework just keeps on improving.