CarrierWave 如何在给定的 url 存储文件

发布于 2024-12-03 11:01:38 字数 368 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-12-10 11:01:39

在用户注册后的控制器中(假设您的用户图像字段被简单地称为“图像”),

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

我认为这是根据 载波文档 和一些搜索。

享受!

in the controller after user sign up (assuming your user image field is called simply 'image')

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

i think this is the best way according to carrierwave docs and some searching around.

enjoy!

过去的过去 2024-12-10 11:01:39

实际上,如果您使用活动记录/模型助手,您可以使用内置的 remote_{attribute}_url 属性来执行此操作(有关详细信息,请参阅 CarrierWave Railscast)。然而,我在源代码中进行了一些挖掘,看看它实际上是如何工作的,即使您不是,您也应该能够使用以下内容:

uploader = ImageUploader.new
uploader.download! some_remote_url
uploader.store!

尝试一下。

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:

uploader = ImageUploader.new
uploader.download! some_remote_url
uploader.store!

Give it a try.

依 靠 2024-12-10 11:01:39

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

对于 URL,您需要首先require 'open-uri',然后打开文件/url。像这样的东西应该可以工作:

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

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.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文