Using ActiveStorage with your Rails app

Jimmy Cheung
2 min readOct 5, 2020

When you use images with your Rails app, you can do it in several different ways. One way is easy — just put your images in the ‘app/assets/images’ directory. Then, you can utilize the Rails asset pipeline to access your image. But what if you want to do more with your images? What if you want to use Amazon AWS, Microsoft Azure, or Google Cloud Storage to store your images? Then, you can use Active Storage.

Active Storage Setup

First, run these two commands to set up Active Storage in your Rails app:

bin/rails active_storage:install
bin/rails db:migrate

This will generate and migrate the database required for the storage of these images.

Then, you can configure the ‘config/storage.yml’ file. If you have access to cloud services of any sort, you can set them up here with access keys:

local:
service: Disk
root: <%= Rails.root.join("storage") %>

test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

amazon:
service: S3
access_key_id: ""
secret_access_key: ""
bucket: ""
region: "" # e.g. 'us-east-1'

Then, you can change the setting of your environment ‘config/environments/development.rb’ to tell your rails app which service to use. To begin with, stick with local.

config.active_storage.service = :local

You can change this to use one of the other configs you set up in storage.yml.

Setting up the model

Now, in your models in ActiveRecord, you can now use the ‘has_one_attached’ macro to ensure each User can be associated with an image.

class User < ApplicationRecord
has_one_attached :avatar
end

If you require multiple images, you can use ‘has_many_attached’ instead.

class Message < ApplicationRecord
has_many_attached :images
end

Using Active Storage for submitted images

Now that you have ActiveStorage set up with you models, you can begin with a couple ways of how to use ActiveStorage. One of the most common ways is to allow someone to upload images. To do so, simply put on your Rails web page a request for the file_field in question:

<%= form.file_field :avatar %>

Then, in your Rails code, you can call upon the ‘attach’ method to attach the file:

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

You can also check if a user already has an attachment with this method:

user.avatar.attached?

Using Active Storage with existing images

Sometimes you have existing images you want to associate with your models. You can do with the attach command again.
However, you will specify the io this time as well as the file in question.

@message.image.attach(io: File.open('/path/to/file'), filename: 'file.pdf', content_type: 'application/pdf')

Conclusion

ActiveStorage is a useful tool for Rails apps and can definitely power a powerful image use case. The fact that you can simply use or switch over to Amazon or other cloud providers as well is really powerful and can allow your application to scale. What is covered here is the tip of the iceberg and coverage of basic use cases. If you want to learn more, review the Rails docs for Active Storage.

Sources

--

--