⚡
LightningRails
  • 👋Welcome
  • Access the Repo
  • Getting Started
    • 🚀Quickstart
    • 🎨Themes
    • 🖼️Customize the views
    • 🔮DaisyUI Library
    • ⚡Lightning Landing
      • Quickstart
      • Theme and branding
      • Page structure
      • Publish your landing page
  • Features setup
    • 📸Images & media
    • 🔐Admin Dashboard
    • Search Engine Optimization
    • 📧Automatic Emails
      • 🟨Postmark
      • 🔲Resend
    • 🚪Login with Devise
    • 🪄Magic Link Signup
    • 💳Stripe Payment Gateway
    • Github Signup
    • Lucide icons
    • 🤖Multi-provider AI
    • Open AI API
    • 🧙‍♂️Multi-Step Form Wizard
  • UI Components
    • 🦸Heros
    • ❔FAQs
    • 🃏cards
    • 💬Testimonials
    • 👋Call To Actions
    • 🔦Features
  • Deploying to production
    • ⬆️Heroku Deploy
    • 🛡️Security
      • 🎛️Rate Limiting
  • RESOURCES
    • 🚀Vote for new features
    • Report an issue
    • 🆘Get help on Slack
    • 🍭Design Resources
      • Maria Ba Illustrations
      • Assets Mockup Generator
      • Logo Generator
      • Tailwind Cheatsheet
      • HyperUI Tailwind Library
Powered by GitBook
On this page
  • Image Processing and Storage with Cloudinary
  • Setting up Cloudinary in Your Application
  • Installing Active Records
  • Attaching Images to Your Models
  • Displaying the image in the view

Was this helpful?

  1. Features setup

Images & media

PreviousPublish your landing pageNextAdmin Dashboard

Last updated 5 months ago

Was this helpful?

Image Processing and Storage with Cloudinary

Welcome to the Image Processing and Storage section of our wiki! In this guide, we will explore how to efficiently handle image management within our application using Cloudinary.

Cloudinary is a powerful cloud-based service that provides an extensive suite of tools for image upload, storage, and manipulation. It allows developers to easily integrate image processing capabilities into their applications, making it simple to transform, resize, and optimize images on-the-fly. And the best part is it has a very generous free plan as the free credits renew monthly 🎉

By leveraging Cloudinary, we can enhance our application's performance and improve the user experience through faster image delivery and efficient management. Let's get started!

Setting up Cloudinary in Your Application

# .env
CLOUDINARY_URL=cloudinary://298522699261255:***********************8@Qa1ZfO4syfbOC # Your Key

Installing Active Records

Active Storage enables the upload of files to cloud storage services, such as Cloudinary, and allows you to link these files to Active Record models.

rails active_storage:install
rails db:migrate

This sets up two database tables to manage the relationships between files uploaded to Cloudinary and any model within our application.

Attaching Images to Your Models

Once Cloudinary is set up, you can easily attach images to your models using Active Storage or another file attachment library. This section will cover how to associate image uploads with your database models, ensuring that your images are properly linked and accessible.

Setup for one image

# models/car.rb
class Car < ApplicationRecord
  has_one_attached :photo
end

In your controller:

# app/controllers/cars_controller.rb
def car_params
  params.require(:car).permit(:title, :photo)
end

In your view:

<!-- app/views/cars/_form.html.erb -->
<%= form_for(car) do |f| %>
  <!-- [...] -->
  <%= f.file_field :photo %>
  <!-- [...] -->
<% end %>

Setup for multiple images

# models/car.rb
class Car < ApplicationRecord
  has_many_attached :photos
end

In your controller:

# app/controllers/cars_controller.rb
def article_params
  params.require(:car).permit(:title, photos: [])
end

In your view:

<!-- app/views/cars/_form.html.erb -->
<%= simple_form_for(car) do |f| %>
  <!-- [...] -->
  <%= f.input :photos, as: :file, input_html: { multiple: true } %>
  <!-- [...] -->
<% end %>

Displaying the image in the view

To display the image on your view, we will need to change the image_tag by cl_image_tag and call its key:

<%= cl_image_tag @car.photo.key, height: 500, width: 500, crop: :fill %>

Et voila! 🥖 You have a free cloud provider for your rails images.

To get started with Cloudinary, you need to , find your API key, and add it to your .env file in your LightningRails application.

📸
create an account on their website
Cloudinary gives you many credits for free, renewed monthly. Basically Free for MVPs 🍻