What is Active Storage?
Active Storage is a Rails framework for attaching files to Active Record models. It's been part of Rails since version 5.2 and handles the plumbing for uploading files, storing them in cloud services, and retrieving them later — without third-party gems like Paperclip or CarrierWave.
How do I handle media uploads in a Rails app?
Use Active Storage. Install it once, declare has_one_attached or has_many_attached on the model, add a file_field to the form, and point config/storage.yml at Disk in development and at S3, Google Cloud Storage, Azure or an S3-compatible service like Cloudflare R2 in production. Turn on direct uploads for large files, so the browser sends them straight to storage instead of through your app. Each step is below.
To link to an upload, use url_for(user.avatar). Use rails_blob_path(user.avatar, disposition: "attachment") when the link should download the file instead of showing it.
Setup
Add Active Storage to your app (if not already configured):
rails active_storage:install
rails db:migrate
This creates two tables: active_storage_blobs (metadata about each file) and active_storage_attachments (the polymorphic join between blobs and your models).
Configure a storage service in config/storage.yml:
amazon:
service: S3
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
region: us-east-1
bucket: your-bucket-name
Tell Rails which service to use in config/environments/production.rb:
config.active_storage.service = :amazon
Attaching files to models
class User < ApplicationRecord
has_one_attached :avatar
has_many_attached :documents
end
# Attach a file
user.avatar.attach(params[:avatar])
# Check if attached
user.avatar.attached?
# Get the URL
url_for(user.avatar)
Image variants
Active Storage can transform images on the fly using image_processing:
# In your view
image_tag user.avatar.variant(resize_to_limit: [100, 100])
Direct uploads
For large files, Active Storage supports direct uploads — the browser uploads directly to the cloud service, bypassing your Rails server:
<%= form.file_field :avatar, direct_upload: true %>
Using Active Storage with Avo
Avo has built-in support for Active Storage fields. You can use file and files fields in your Avo resources:
class Avo::Resources::User < Avo::BaseResource
def fields
field :avatar, as: :file, is_image: true
field :documents, as: :files
end
end
Supported storage services
- Amazon S3 — most commonly used in production
- Google Cloud Storage — good option if you're already on GCP
- Microsoft Azure Storage — for Azure-based deployments
- Disk — local filesystem, suitable for development and test
- Mirror — replicate uploads across multiple services simultaneously