1

I want to attach an image to a record in the rake task. The only thing I have for image is the URL. This is the URL for the image https://cdn.iconscout.com/icon/free/png-256/google-analytics-7-722699.png.

I used this approach from a StackOverflow answer.

require 'open-uri'
downloaded_file = open(tool.image)
new_tool.image.attach(io: downloaded_file, filename: "foo.png")

This gives the following error.

ActiveStorage::IntegrityError: Unsupported source URL: #<StringIO:0x00007f9bd9759f28>
Caused by:
CloudinaryException: Unsupported source URL: #<StringIO:0x00007f9bd9759f28>

Could someone help me out?

Salman Haseeb Sheikh
  • 1,122
  • 2
  • 12
  • 20

1 Answers1

1

In this case, I think that you should pass a string containing the image's URL to open:

require 'open-uri'

image_url = "https://cdn.iconscout.com/icon/free/png-256/google-analytics-7-722699.png"    
downloaded_file = open(image_url)

new_tool.image.attach(io: downloaded_file, filename: "foo.png")