【发布时间】:2018-12-04 07:06:22
【问题描述】:
我希望使用 rails 5.2 主动存储来保存位于另一个 http web 服务器上的文件(在本例中为图像)。
我有一个带有源 url 字符串参数的对象。然后在 before_save 上,我想抓取远程图像并保存它。
示例:图片的 URL http://www.example.com/image.jpg。
require 'open-uri'
class User < ApplicationRecord
has_one_attached :avatar
before_save :grab_image
def grab_image
#this indicates what I want to do but doesn't work
downloaded_image = open("http://www.example.com/image.jpg")
self.avatar.attach(downloaded_image)
end
end
提前感谢您的任何建议。
【问题讨论】:
标签: ruby-on-rails rails-activestorage