DHH announced earlier in the year that Rails 5.2 will have a brand new component called ActiveStorage. ActiveStorage will manage user uploads such as photos directly by Rails.
Since then, a lot of progress has been made to integrate ActiveStorage to Rails, and you can actually use ActiveStorage today. This blog will explore the how you can update your Rails app to use ActiveStorage.
Warning! You will be using the bleeding edge version of Rails and there may be unforseen problems caused by it.
Setting up ActiveStorage for Rails
1. If you are using Rails version before 5.1.14, update Rails to 5.1.14 by updating your Gemfile
$ bundle update rails
$ rails app:update and resolve any code differences.
4. Test that updating to
5.1.14 went ok.
5. Now we want to update rails to the latest bleeding edge version. In the
Gemfile, add the following lines.
$ bundle update rails
$ bundle exec rails -v should display the bleeding edge version,
8. Update the application configs. Run
$ bundle exec rails app:update.
9. When you run,
$ ./bin/rails --tasks, you should see,
rails active_storage:install as an available task.
$ ./bin/rails active_storage:install. This should generate a migration file.
$ ./bin/rails db:migrate. Now our sqlite db should be ready to support ActiveStorage.
A simple image uploading example using ActiveStorage
Up until now, we’ve been preparing Rails application to support ActiveStorage. Now that the application is equipped with ActiveStorage, let us build a simple feature that allows us to create a post with images that uses ActiveStorage.
1. Generate post model.
$ ./bin/rails g model post.
2. Let’s add two columns to post table,
body in the migration file.
3. Let’s create a controller for posts resource.
$ ./bin/rails g controller posts
4. And add posts resources routes in the
5. We need to associate post with images.
6. Let’s add code for index, show and create actions.
7. Add the index view for the post with the following code. This will display upload form on localhost:3000/posts page.
8. Now let’s add a view for show post.
9. Try submitting a photo and you will see the show view displays the image you just submitted.
The image is currently stored locally in a
storage in the app root level. But you can configure
this to push the files to cloud file storage systems like AWS S3, Google Cloud
As you can see, ActiveStorage presents a simplied file management system that is well integrated with Rail’s ActiveRecord component. As ActiveStorage matures, we should see majority of file management use cases being covered by it. While there are other file management systems out there for Rails, I suggest that you consider ActiveStorage before you explore other options.
Also check out the documentation on ActiveStorage.
You can see the final working code on the github repo.
I wanna thank @jeffreyguenther who shared his experience of using ActiveStorage on his projects with me.