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.
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.
- Uploading file to Amazon S3
- Uploading files to Google Cloud Storage
- Uploading files directory to Amazon S3 / Google Cloud Storage
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.
- Rails >= 5.1
1: Create a new Rails Project.
rails new activestorage-demo
2: Add activestorage to Gemfile.
3: Bundle install.
Perform bundle install so that Gem is installed and configured to be used with the Rails application.
4: Installation and Usage
activestorage in the
Run the activestorage install command.
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.
pg gem to the Gemfile.
Perform bundle install on the application.
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
It needs to be changed with
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
to exist in
6: Test file upload
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.
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.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox