如何在Rails Active Storage中从url中附加图片?

问题描述 投票:0回答:1

我想在rake任务中把一个图片附加到一个记录上。我对图片的唯一要求就是URL。这是图片的URL https:/cdn.iconscout.comiconfreepng-256google-analytics-7-722699.png。.

我从StackOverflow的一篇文章中使用了这个方法。回答.

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

这就出现了以下错误。

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

谁能帮帮我?

ruby-on-rails cloudinary rails-activestorage
1个回答
0
投票

在这种情况下,我认为你应该把包含图片URL的字符串传递给 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")
© www.soinside.com 2019 - 2024. All rights reserved.