64
loading...
This website collects cookies to deliver better user experience
Gemfile
insert the following line:gem 'cloudinary'
bundle
.config
folder called cloudinary.yml
development:
cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
enhance_image_tag: true
static_file_support: false
It's good to make use of environment variables to put your credentials in this file. You can easily manage env variables using the dotenv gem
If you're gonna handle image/video or PDF previews, you'll also need other libs.
Your can see the full list of requirements here.
active_storage_blobs
, active_storage_variant_records
and active_storage_attachments
.rails active_storage:install
to generate the migration file and as always, run rail db:migrate
.config/storage.yml
to create a cloudinary service.cloudinary:
service: Cloudinary
folder: MEDIA_FOLDER_NAME_YOU_CREATED_IN_CLOUDINARY
Don't forget to change MEDIA_FOLDER_NAME_YOU_CREATED_IN_CLOUDINARY with a valid folder name you setup in your cloudinary dashboard.
config/environments/development.rb
put the following inline:config.active_storage.service = :cloudinary
Note: This line is probably already declared in this file as config.active_storage.service = :local
, so you just to replace the assignment with cloudinary
.
Publication
entity that the user can create with a thumbnail
image.has_one_attached :thumbnail
to your publication.rb
file.This is a case where the entity has ONE file associated with a column.
If you need to storage and handle multiple images take a look has_many_attached
<%= form.file_field :thumbnail %>
@publication = News.create params # any other param your entity may need
@publication.thumbnail.attach(params[:thumbnail])
<img src="<%= publication.thumbnail.attachment.url %>" alt="A nice alt value" />
64