CarrierWave 如何在给定的 url 存储文件
我的 CarrierWave 通过典型的 ORM 设置和通过表单上传运行良好。我想弄清楚如何在表单提交上下文之外使用 CarrierWave。例如,当用户注册时,我想抓取他们的头像并将其存储在 CarrierWave 中。这是我所拥有的,但它不起作用:
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
也没有错误。我一直在网上浏览,但未能找到解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在用户注册后的控制器中(假设您的用户图像字段被简单地称为“图像”),
我认为这是根据 载波文档 和一些搜索。
享受!
in the controller after user sign up (assuming your user image field is called simply 'image')
i think this is the best way according to carrierwave docs and some searching around.
enjoy!
实际上,如果您使用活动记录/模型助手,您可以使用内置的
remote_{attribute}_url
属性来执行此操作(有关详细信息,请参阅 CarrierWave Railscast)。然而,我在源代码中进行了一些挖掘,看看它实际上是如何工作的,即使您不是,您也应该能够使用以下内容:尝试一下。
Actually you can do this using the built in
remote_{attribute}_url
property if you are using the active record/model helpers (see the CarrierWave railscast for the details). However, I dug around in the source code a bit to see how this actually works and it appears that even if you are not you should be able to use the following:Give it a try.
我在尝试弄清楚如何让
store!
使用本地文件路径时遇到了很多麻烦。事实证明,store!
实际上采用文件作为参数,而不是字符串。对于 URL,您需要首先
require 'open-uri'
,然后打开文件/url。像这样的东西应该可以工作:同样可以使用文件路径,但在这种情况下您不必要求
open-uri
。I've had lots of trouble trying to figure out how to get
store!
to work with local file paths. It turns out thatstore!
actually takes a file as a parameter, not a string.For the URL, you'll need to
require 'open-uri'
first, then open the file/url. Something like this should work:The same will work with a file path, but you don't have to require
open-uri
in that case.