是否可以使用PaperClip通过url获取图像? fleximage是可能的,但是fleximage不支持Rails3,因此我切换到回形针。
目前,我正在通过curl获取图像,将其保存在硬盘上,并通过回形针的image_file加载。
我没有通过Google找到真正的解决方案,因此希望您能为我提供帮助。
最佳答案
是的,这是可能的,而且非常简单。
在您的模型中:
#we use this to get the image.
require 'rest-open-uri'
Class Model << ActiveRecord::Base
has_attached_file :picture
#Get the picture from a given url.
def picture_from_url(url)
self.picture = open(url)
end
然后,您可以执行以下操作:
#controller
@model.picture_from_url(<Your URL here>)
并且因为我们将图像与对象的其余部分一起保存。您可以在自己的 View 中使用它:
<%= image_tag @model.picture.url %>
希望这可以帮助!
关于ruby-on-rails - 回形针直接通过网址获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4830122/