Paperclip直接通过url获取图像
可以使用PaperClip通过URL获取图像吗? 灵活成像是可能的,但fleximage不支持Rails3,所以我已经切换到回形针。
目前,我通过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()
因为我们用对象的其余部分保存了图像。 您可以在视图中使用它:
<%= image_tag @model.picture.url %>
希望这可以帮助!
方法不能满足您的需求吗?
换句话说,如果您的模型被称为Foo
,并且您将其设置为has_attached_file :bar
那么foo.bar.url
应该提供您的图像的URL,您可以将其放入image_tag
或link_to
或任何您想要的内容。
如果那不是你想要的,你能澄清一下你的意思吗?