Paperclip 直接通过 url 获取图像 [英] Paperclip fetch image directly via url

查看:46
本文介绍了Paperclip 直接通过 url 获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 PaperClip 通过 url 获取图像?使用 fleximage 是可能的,但 fleximage 不支持 Rails3,所以我切换到了回形针.

It's it possible to fetch an image via url using PaperClip ? It's possible with fleximage, but fleximage has no support for Rails3 so I've switched over to paperclip.

目前,我是通过 curl 获取图片,将其保存在硬盘上,然后通过回形针的 image_file 加载它.

At present, I'm fetching the image via curl, saving it on the hdd and load it via image_file of paperclip.

我没有通过谷歌找到真正的解决方案,所以希望你能帮助我.

I've found no real solution via google, so hopefully you can help me.

推荐答案

是的,这是可能的,而且非常简单.

Yes this is possible and amazingly simple.

在您的模型中:

#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

然后你可以做这样的事情:

And then you can do something like this:

#controller
@model.picture_from_url(<Your URL here>)

因为我们将图像与对象的其余部分一起保存.你可以在你的视图中使用它:

And because we saved the image with the rest of the object. You can just use this in your views:

<%= image_tag @model.picture.url %>

希望这会有所帮助!

这篇关于Paperclip 直接通过 url 获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆