简体   繁体   English

Rails回形针将图像从URL保存到数据库

[英]Rails Paperclip Save Images from URL into Database

I have a Rails app with underlying MySQL database containing many products. 我有一个包含许多产品的基础MySQL数据库的Rails应用。 Each product has a picture, which is currently stored in the database as URL. 每个产品都有一张图片,当前以URL的形式存储在数据库中。

How can I save these with paperclip on S3. 如何在S3上使用回形针保存这些内容。 How do I store the filename in my MySQL database? 如何将文件名存储在MySQL数据库中? And how do I trigger the refresh, once I updated my database with hundreds of new products? 用数百种新产品更新数据库后,如何触发刷新?

Thanks a lot for help! 非常感谢您的帮助!

You will need to download the image from the url and let paperclip do the reprocessing for you. 您将需要从url下载图像,然后让回形针为您进行重新处理。 You can find code to download the image on paperclip's wiki https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL 您可以在回形针的wiki https://github.com/thoughtbot/paperclip/wiki/Attachment-downloaded-from-a-URL上找到下载图像的代码

For the sake of the example, let's say you have Users with avatars, rather than products with images and the existing field is old_avatar_url . 就本例而言,假设您的用户具有头像,而不是具有图像的产品,并且现有字段为old_avatar_url Setup User to store an avatar, as instructed by paperclip . 设置用户按照回形针的指示存储化身。

User.find_each do |user|
  user.avatar_remote_url = user.old_avatar_url
  user.save
end

After than you can safely remove the old_avatar_url column. 之后,您可以安全地删除old_avatar_url列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM