Rails Uploading Images And Rich Text With ActiveStorage
Techie November 2022
Introduction
Image upload in Rails is easy to implement with ActiveStorage.
We can also implement action_text for image uploading but also as a toolkit for
handling rich text content & editing and file attachments in general.
In this section, we are going to be setting up ActiveStorage and action_text to
upload images to our Rails application in simple steps.
Prerequisites
Rails 7.0.4 Ruby 3.2.0
Part A: Install and Configure ActiveStorage
1. Install active_storage and action_text
ii). Uncomment image_processing gem in Gemfile
iii). Bundle up
2. Install ImageMagick
Active storage will require image processing to resize the images.
vips is the default image processor but we’ll use imagemagick.
i) Switch image processing from vips to image_magick. Add this code in
/config/environments/development.rb
ii) Install ImageMagick
The chances that ImageMagick has already been installed on your Ubuntu system
are pretty high. Confirm this by checking its version:
You’ll get an error message if it is not installed. Install it with the following command:
Part B: Usage Example
1. Create a person scaffold
i). Generate the scaffold
ii). Add the route
2. Associating the Model With ActiveStorage
i). Set up image upload for the person.rb model
ii). White-list the image params in the persons_controller.rb:
iii). Resizing The Images
Create a method for resizing the images in person.rb