Using Active Storage on your Ruby on Rails App
ActiveStorage allows uploading files without writing much code. It even allows you to perform some complex tasks easily, such as uploading a high resolution image, and creating several low resolution variants of the same image. However, you need to ensure that you set up all requirements properly, especially if your app is going to be deployed to a cloud service, such as, Heroku.
If you are using variants method that comes built in with ActiveStorage, you will need to ensure you have the
ruby-vips gem in your Gemfile and some buildpacks necessary to process images.
Adding Heroku Buildpacks
Add the needed buildpacks to Heroku. This is important because Active Storage relies on certain libraries being available in your host environment. When you are running the app on your Mac, this is typically a bunch of stuff installed on your Mac. But when you deploy it to cloud, your cloud environment needs those libraries too. That is where Buildpacks come in, if you are pushing your code to Heroku.
There are two ways to do this, either through your app.json file if you use that for deployment, or directly through Heroku CLI, or terminal. Here are both ways.
Installing Buildpacks Through Heroku CLI
If you have Heroku CLI installed, you can connect with your app and install the buildpacks needed to enable certain image processing capabilities in your app directly. Run the commands below to add the two additional buildpacks needed (in addition to the ruby buildpack).
heroku buildpacks:set --index=1 heroku-community/apt -a [app-name]
heroku buildpacks:set --index=2 https://github.com/brandoncc/heroku-buildpack-vips -a [app-name]
heroku buildpacks:set --index=3 heroku/ruby -a [app-name]
Updating app.json File for Heroku Review App Deployment
If you have been using Heroku Review apps, then you will also need to update the buildpacks section of the app.json directive to ensure that these buildpacks are deployed correctly to spin up your review apps. Else, review app builds will start failing, and you will not be able to test the Active Storage capabilities, especially the variants methods in your app to create different variants of your original image.
"postdeploy": "bundle exec rake db:migrate db:seed"
Add Aptfile to Include Libraries Needed
Create a file named Aptfile in the root directory of your app, and add the following lines in it:
This will ensure you have access to the libraries needed for any image processing using vips.
Update Github Actions for Proper CI Deployment and Testing
Finally, if you use Github Actions for CI, then you will want to update the yml file to include the directive to install
libvips before invoking any tests on the CI setup.
- name: Install libvips
run: sudo apt-get install -y libvips
Hopefully, the above information helps you modify your Rails app configuration correctly to ensure that the app runs without any issues after you have Active Storage enabled.