Picture of the author

GAURAV VARMA


Rails 5.2 introduced ActiveStorage, a built-in solution for handling file uploads in Rails applications. This long-awaited feature brought native support for uploading files to services like Amazon S3, Google Cloud Storage, and Microsoft Azure.

Why ActiveStorage?

Before ActiveStorage, developers often relied on third-party gems like CarrierWave or Paperclip. While powerful, they required significant configuration and integration. ActiveStorage is now part of Rails itself, providing:

  • Easy setup and use
  • Integration with cloud services
  • Support for file variants (e.g., thumbnails)
  • Direct uploads from the browser

Setting it up

To get started, you run:

1rails active_storage:install
2rails db:migrate

This creates the necessary database tables.

In your model:

1class User < ApplicationRecord
2  has_one_attached :avatar
3end

Uploading a file

In your form:

1<%= form.file_field :avatar %>

Then attach it like:

1@user.avatar.attach(params[:avatar])

Variants

You can process uploaded images:

1<%= image_tag user.avatar.variant(resize_to_limit: [100, 100]) %>

Links

Summary

ActiveStorage modernized file uploads in Rails, giving developers a powerful and integrated toolset for managing attachments, variants, and direct cloud storage without third-party gems.