SRM — Rails React App with S3 Active Storage deployed on Heroku

Jeni Dang
5 min readJul 15, 2022

“Never ever judge anyone. You don’t know their story and what’s really going on. A smile can hide so much.” — Jean Horvy

This is absolutely true, even for a piece of dessert. SRM is a small bakery shop that specializes in giving a story to every piece of dessert that they make. All their products can be customized to any type of events.

With this meaningful story and business model, SRM app was born. SRM app is built with Rails backend and React frontend with Amazon S3.

Here’s a quick tour of SRM app:

Landing/Homepage: top 3 ordered products automatically update based on customers ordered
(no user is log in) Product List: display all products available/selling
(no user log in) Product Detail: Display the selected product information and review, if there is a review for the selected product.
Contact Form: Send a message to SRM team member. This feature is connected to EmailJS.
Login/Signup Form (Credit to CoderOne for this wonderful design)
Product List with Add To Cart button when user sign in
Product Detail: Show/Hide Review Form for user to add reviews to a selected product
Cart (left image): show products user added to their cart — Event Form (right image): fill out the detail for the event the order is for
Checkout: Order information, send to SRM team and a confirmation letter email to the user
Admin Profile (left image): shows all the products posted to sell and sales detail for the product — New Product Form (right image): allow admin user to add new products to the app

Now that we see how SRM app looks like, let’s talk about S3 Active Storage and what to do to ensure that we don’t have an issue deploying the app to Heroku.

  1. Create an account with Amazon AWS
  2. Sign in -> click Services -> click S3 -> click “Create bucket” -> Enter a Bucket Name -> select your AWS Region -> keep all the settings -> scroll to the bottom -> click “Create bucket”
  3. Go back to your project file and update Gemfile:
gem "image_processing", ">= 1.2"gem 'aws-sdk-s3'gem 'dotenv-rails'

4. In the terminal, run

bundle install 

5. Add a file to the root of your project, name it “.env”

The .env file is typically used for the configuration of your application, which normally includes sensitive information such as credentials etc.

6. Add all your secure AWS information into the .env file

7. Locate the gitignore file in the root of your project and update the file with .env (gitignore file tells Git which files to ignore when we push our project codes to the GitHub repo).

8. Go to config/storage.yml and update the file

9. Go to config/environments/development.rb and update the file with:

config.active_storage.service = :amazon

10. Go to config/environments/production.rb and update the file with:

config.active_storage.service = :amazon

11. Go to config/application.rb and add:

require "active_storage/engine"

12. In the terminal run

rails active_storage:install

Active Storage will create a migration for you in db/migrate that have all the required setup that you needed:

example of CreateActiveStorageTables

13. Now in the terminal run

rails db:migrate

13. Add Association to models/product.rb with:

has_one_attached :image #you can name this whatever you want :photo, :avatar, :img, etc... 

14. Update the serializers/product_serializer.rb with a helper method to create a url for the image upload:

include Rails.application.routes.url_helpersdef image_url
return nil unless object.image.attached?
object.image.blob.attributes.slice('filename',
'byte_size').merge(url: rails_blob_path(object.image,
only_path: true)).tap { |attrs| attrs['name'] =
attrs.delete('filename') }
end

15. Update the prooduct_params in the controllers/api/products_controller.rb to include image:

privatedef product_params
params.require(:product).permit(:user_id, :name, :price,
:description, :category, :image)
end
React-Client side form for user to use to upload an image from their local computer

Now, you can start using image upload to add image to your product. Once the image is uploaded, you will see a url:

To display the image, simply add it to your code:

That is it!

If you follow through these steps to have S3 Active Storage setup correctly at the beginning of your project building, once you’re ready to deploy to Heroku, it will be like putting icing on a cake.

Helpful resources:

--

--