Exploring Rails ActiveStorage for Managing Media Files
Learn how to efficiently handle file uploads in Rails using ActiveStorage
Introduction
Handling file uploads and media storage efficiently is a crucial part of web applications. Ruby on Rails provides ActiveStorage, a built-in solution that simplifies file uploads, supports cloud storage services, and integrates well with image processing libraries.
In this guide, we’ll cover:
✔️ Setting up ActiveStorage in Rails
✔️ Uploading and retrieving files
✔️ Integrating cloud storage (AWS S3, Google Cloud, Azure)
✔️ Image transformations and processing
✔️ Optimizing performance and security
1. What is ActiveStorage?
ActiveStorage is Rails’ built-in file attachment system, introduced in Rails 5.2, to replace gems like CarrierWave and Paperclip.
✔️ Supports local and cloud storage (AWS S3, Google Cloud, Azure)
✔️ Handles direct file uploads to cloud storage
✔️ Provides easy-to-use helpers for attaching and displaying files
✔️ Supports image processing via MiniMagick or Vips
2. Setting Up ActiveStorage in a Rails App
Step 1: Install ActiveStorage
Run the following command to generate ActiveStorage tables:
rails active_storage:install
rails db:migrate
This creates tables (active_storage_blobs
, active_storage_attachments
) to store metadata about uploaded files.
Step 2: Configure Storage Service
Modify config/storage.yml
to define storage services.
✔️ Local Storage (default)
local:
service: Disk
root: storage
✔️ Amazon S3 Storage
amazon:
service: S3
access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>
secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
region: us-east-1
bucket: my-app-bucket
Set the storage service in config/environments/production.rb
:
config.active_storage.service = :amazon
3. Attaching Files to Models
To allow file uploads, use the has_one_attached
or has_many_attached
helper in models.
Single File Upload Example
class User < ApplicationRecord
has_one_attached :avatar
end
Multiple File Upload Example
class Post < ApplicationRecord
has_many_attached :images
end
4. Uploading Files in Controllers and Forms
Updating File in Controllers
def update
@user = User.find(params[:id])
@user.avatar.attach(params[:avatar])
redirect_to @user
end
File Upload Form Example
<%= form_with model: @user, local: true do |form| %>
<%= form.file_field :avatar %>
<%= form.submit "Upload Avatar" %>
<% end %>
5. Displaying and Processing Uploaded Files
Rendering an Image in Views
<%= image_tag user.avatar if user.avatar.attached? %>
Generating a Variant (Resizing Images)
<%= image_tag user.avatar.variant(resize: "100x100") %>
💡 Requires MiniMagick or Vips for image processing:
gem install image_processing
6. Direct Uploads to Cloud Storage
For large file uploads, direct cloud uploads improve performance and scalability.
Enable Direct Uploads
Add this to app/javascript/packs/application.js
:
import * as ActiveStorage from "@rails/activestorage"
ActiveStorage.start()
In your form:
<%= form.file_field :avatar, direct_upload: true %>
🚀 This allows the browser to upload files directly to Amazon S3, Google Cloud, or Azure, bypassing the Rails server.
7. Securing and Optimizing File Uploads
Restricting File Types
validates :avatar, content_type: ['image/png', 'image/jpeg']
Limiting File Size
validates :avatar, size: { less_than: 5.megabytes }
Preventing Unauthorized Downloads
To serve files only to authorized users, use redirect_to
:
def show
user = User.find(params[:id])
redirect_to user.avatar.url if user.avatar.attached?
end
8. Cleaning Up Old Files
Unused files take up storage and should be deleted when an object is destroyed.
class User < ApplicationRecord
has_one_attached :avatar
before_destroy :purge_avatar
def purge_avatar
avatar.purge
end
end
Conclusion
Rails ActiveStorage provides a powerful and flexible way to handle media uploads. With cloud integration, direct uploads, and processing support, it simplifies file management in Rails applications.
🚀 Next Steps:
🔹 Use ActiveStorage for file uploads and image transformations
🔹 Integrate with AWS S3, Google Cloud, or Azure
🔹 Optimize performance and security for media files