Rails 5.2 introduces Active Storage for File Uploads

DHH [Creator of Rails] has recently announced Active Storage as a built in support for file uploads. Active Storage is targeted to be released with Rails 5.2. The development has been started on Github in ActiveStorage repository. It is started as a separate repository for now. This ActiveStorage is eventually planned to be merged in Rails repository.

rails-active-storage

Cloud Storage Support

Right now, Rails Active Storage plans to support following Cloud Storage Players.

  • Amazon AWS
  • Google Cloud Storage
  • Azure Storage [Work in Progress here]

Rails File upload ecosystem

As of now, people using Rails are dependent on the gems given below.

The above gems solve the problem of file handling. But, there has been no standardization when it comes to having support for the common functionalities as given below.

For some of the implementation part, we also need to be dependent on Fog-AWS or Fog-Google

Direct Uploads to Cloud Storage

Some people need the solution where they do not want to process image/file upload through the application server.

In such cases, we need to have the support to upload image/file asset directly to the cloud. Rails Active Storage has already merged in a pull request starting off the work to directly upload files to cloud.

Instead of wandering through these different gems, it all will be at a single place with the introduction of Rails Active Storage.

Active Storage Demo Application

We can try and integrate the development version of Active Storage by following the steps given below.

Prerequisites

  • Rails >= 5.1

Steps

1: Create a new Rails Project.

rails new activestorage-demo

2: Add activestorage to Gemfile.

gem 'activestorage'

3: Bundle install.

bundle install

Perform bundle install so that Gem is installed and configured to be used with the Rails application.

4: Installation and Usage

Require activestorage in the application.rb

require "active_storage"

Run the activestorage install command.

rails activestorage:install

The above command will create necessary migrations of tables that will be used to store the objects.

Given below is the sample of how activestorage install creates a migration file.

4: Database configuration

We will use postgresql for the demonstration purpose. Add pg gem to the Gemfile.

gem 'pg'

Perform bundle install on the application.

bundle install

Modify database.yml database configuration file with the database settings.

5: Create Model

Let’s create a model to test out the activestorage gem. We create a users table with following attributes.

  • name (String)
  • image (File)
rails g migration create_users name:string

The above command will create migration as given below.

Then, create database with the following command.

bundle exec rails db:create:all

Run the migrations with rails db migrate command.

bundle exec rails db:migrate

When we run the migrations, an error is thrown regarding migration name.

Update migration file name

The migration name is ActiveStorage::CreateTables. It needs to be changed with ActiveStorageCreateTables.

Then, run the migrations again with the command given below.

bundle exec rails db:migrate

Update storage_services.yml file

Now, if you start the server you may come across an error on boot.

After debugging a bit into source code, found out that storage_services.yml was expecting secrets of aws configration to exist in secrets.yml file.

When tried to look into the activestorage repository ,found out that the fix is already merged. Use the following storage_services.yml file for the time being until next stable version is released.

6: Test file upload

Let’s add has_one_attached to the attribute that we want to use for storage.

We can try uploading an image to user with the code given below.

We can see it updates the image against the user in the active storage tables.

Conclusion

The ActiveStorage is still in development phase and thus is not stable. Those who are interested can try integrating and contribute to the development. We looked into how to create a demo rails application with activestorage to handle file uploads.

Please let us know through comments your opinion about Rails ActiveStorage integration.