是否可以使用PaperClip通过URL获取图像?使用fleximage是可能的,但由于fleximage不支持Rails3,因此我已切换到paperclip。
目前,我通过curl获取图像,将其保存在硬盘上,并通过paperclip的image_file加载它。
我在谷歌上没有找到真正的解决方案,希望您能帮助我。
是否可以使用PaperClip通过URL获取图像?使用fleximage是可能的,但由于fleximage不支持Rails3,因此我已切换到paperclip。
目前,我通过curl获取图像,将其保存在硬盘上,并通过paperclip的image_file加载它。
我在谷歌上没有找到真正的解决方案,希望您能帮助我。
是的,这是可能的,并且非常简单。
在你的模型中:
#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>)
因为我们将图片与对象的其余部分一起保存了,所以您可以在视图中直接使用它:
<%= image_tag @model.picture.url %>
希望这可以帮到你!
<modelname>.<attachmentname>.url
方法不能满足您的需求吗?
换句话说,如果您的模型名为 Foo
,并将其设置为 has_attached_file :bar
,那么 foo.bar.url
应该会给出您的图像的 URL,您可以将其放入 image_tag
或 link_to
中或者任何您想要的地方。
如果这不是您要找的,请问您能否澄清一下您的意思?