KevinNeub
KevinNeub

Reputation: 53

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.

Upvotes: 3

Views: 1080

Answers (1)

Adim
Adim

Reputation: 1776

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)

Upvotes: 1

Related Questions