[Ruby On Rails] Implementing Image Uploads
What you can learn from this article:
- How to integrate Active Storage and Cloudinary into your Ruby on Rails application for seamless image uploads and management.
Tools in this article:
- Active Storage
- Cloudinary
Background:
Active Storage is a built-in Rails feature that allows you to manage file uploads and associate them with your models. Cloudinary is a powerful cloud-based service designed for managing media assets, such as images and videos. It offers several advantages for developers, especially when working with platforms like Heroku, where the file system is ephemeral.
1. Setting Up Cloudinary
Sign Up
- First, you have to sign up for Cloudinary and get your Cloudinary key.
- Introduce the
dotenv-railsgem for managing sensitive data. - Instructions to create a
.envfile, add it to.gitignore, and store the Cloudinary URL.
Install Cloudinary Gem
# Gemfile
gem "cloudinary"
# terminal
bundle install
Secure API Keys
# Gemfile
gem "dotenv-rails", groups: [:development, :test]
💡
Why do we need
dotenv-rails ?dotenv-rails is a Ruby Gem designed to manage environment variables in your application. By default, Rails does not automatically load the contents of a .env file. This gem helps by reading the variables from the .env file and automatically loading them into your application’s environment. This allows your code to access these variables using the ENV object.- Instructions to create a .env file, add it to .gitignore, and store the Cloudinary URL.
touch .env
echo '.env*' >> .gitignore
# .env
CLOUDINARY_URL=cloudinary://your_key:your_secret@your_cloud_name
2. Active Storage Setup
Install and Configure Active Storage
# terminal
rails active_storage:install
rails db:migrate
💡
How can we check if active storage command is run well?
This command generates the necessary migration files to set up Active Storage in your Rails application.
The generated migration creates two database tables:
This command generates the necessary migration files to set up Active Storage in your Rails application.
The generated migration creates two database tables:
active_storage_blobs: Stores metadata about each uploaded file, such as filename, content type, and byte size.active_storage_attachments: Associates the uploaded files (blobs) with specific models.Configure Cloudinary as the Active Storage Service
# config/storage.yml
cloudinary:
service: Cloudinary
folder: <%= Rails.env %>
- Update the development environment configuration:
# config/environments/development.rb
config.active_storage.service = :cloudinary
3. Integrating Image Uploads
Model Setup
- Add an attachment to the model:
class Article < ApplicationRecord
has_one_attached :photo
end
Form for Image Upload
- Use
simple_formto create a file upload field:
<%= simple_form_for(@article) do |f| %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.input :photo, as: :file %>
<%= f.submit %>
<% end %>
Permitting the Photo in the Controller
def article_params
params.require(:article).permit(:title, :body, :photo)
end
4. Displaying Images in Views
Using Images as Backgrounds
erb
CopyEdit
<div style="background-image: url('<%= cl_image_path(@article.photo.key, height: 300, crop: :fill) %>')">
Article Title
</div>
Show a Single Image
erb
CopyEdit
<%= cl_image_tag @article.photo.key, height: 300, width: 400, crop: :fill %>
5. Conclusion
- Recap what was covered: setting up Cloudinary, Active Storage, and implementing image uploads.
Comments ()