CarrierWave如何在给定的URL中存储文件 [英] CarrierWave how to store file at given url

查看:76
本文介绍了CarrierWave如何在给定的URL中存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让CarrierWave在典型的ORM设置中工作正常,并通过表格上传。我想弄清楚如何在表单提交上下文之外使用CarrierWave。例如,当用户注册时,我想抓住他们的图像并将其存储在CarrierWave中。这是我的东西,它不起作用:

I have CarrierWave working fine through the typical ORM setup and upload via form. I would like to figure out how to use CarrierWave outside of the form submission context. For example, when a user registers I would like to grab their gravatar and store it with CarrierWave. Here's what I have, and it does not work:

gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"

uploader = ImageUploader.new
uploader.store! gravatar_url

也没有错误。我一直在网上浏览,却找不到解决方案。

No error either. I've been looking around the web and have not been able to find a solution.

推荐答案

我遇到了很多麻烦试图弄清楚如何获取 store!来使用本地文件路径。事实证明, store!实际上将文件而不是字符串作为参数。

I've had lots of trouble trying to figure out how to get store! to work with local file paths. It turns out that store! actually takes a file as a parameter, not a string.

对于URL,您首先需要要求输入 open-uri ,然后打开文件/ URL。像这样的东西应该起作用:

For the URL, you'll need to require 'open-uri' first, then open the file/url. Something like this should work:

require 'open-uri'
gravatar_url = "http://www.gravatar.com/avatar/#{Digest::MD5.new.update(current_user.email)}?s=512&d=identicon"
tempfile = open(gravatar_url)    

uploader = ImageUploader.new
uploader.store! tempfile

同样适用于文件路径,但您不必要求 open-uri

The same will work with a file path, but you don't have to require open-uri in that case.

这篇关于CarrierWave如何在给定的URL中存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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