Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveStorage / Cloudinary Not Redirecting Image Request to HTTPS

The ActiveStorage image_url helper generates a URL to the image on the Rails web server. When that request is received on the web server it is redirected to a URL on Cloudinary to request the image. ActiveStorage is generating an image URL with the https protocol but the Rails web server is generating an image URL to Cloudinary with the http (no ssl) protocol. I have not been able to determine why.

The Log of the request:

Started GET "/rails/active_storage/blobs/really_long_hash/user-2.png"
Processing by ActiveStorage::BlobsController#show as JPEG
Parameters: {"signed_id"=>"really_long_hash", "filename"=>"nsi-site-bg"}
ActiveStorage::Blob Load (0.8ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 48], ["LIMIT", 1]]
Cloudinary Storage (4.3ms) Generated URL for file at key: cloudinary_file_name (http://res-4.cloudinary.com/hcfhlrdjg/image/upload/cloudinary_file_name.jpg)
Redirected to http://res-4.cloudinary.com/hcfhlrdjg/image/upload/cloudinary_file_name.jpg
Completed 302 Found in 48ms (ActiveRecord: 13.8ms)

cloudinary.yml

development:
  cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
  api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
  api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
  secure: true
  cdn_subdomain: true
production:
  cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
  api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
  api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
  secure: true
  cdn_subdomain: true
test:
  cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
  api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
  api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
  secure: true
  cdn_subdomain: true

I just updated the cloudinary gem to 1.13.0 and didn't see a change. If you need anything else let me know.

like image 253
KevinNeub Avatar asked Feb 26 '26 02:02

KevinNeub


1 Answers

Cloudinary gem provides a cl_image_tag helper which generates a HTML img tag that links directly to the image on the Cloudinary server

You should prefer to use this helper as opposed to ActiveStorage url helpers as this helper tag generates a direct link to the image and not a link to your server. You get the full benefits of using a CDN by hitting the image directly from Cloudinary. To have https, just specify secure: true in the tag.

E.g: If you have a User record that has_one_attached :file. You can use the cl_image_tag like this:

cl_image_tag(user.file.key, secure: true)
like image 81
Adim Avatar answered Feb 28 '26 16:02

Adim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!