ruby-on-rails - 回形针直接通过网址获取图像

标签 ruby-on-rails paperclip

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

目前,我正在通过curl获取图像,将其保存在硬盘上,并通过回形针的image_file加载。

我没有通过Google找到真正的解决方案,因此希望您能为我提供帮助。

最佳答案

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

在您的模型中:

#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

然后,您可以执行以下操作:
#controller
@model.picture_from_url(<Your URL here>)

并且因为我们将图像与对象的其余部分一起保存。您可以在自己的 View 中使用它:
<%= image_tag @model.picture.url %>

希望这可以帮助!

关于ruby-on-rails - 回形针直接通过网址获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4830122/

相关文章:

ruby-on-rails - Rails 3.x 通过带有 S3::No such file or dir 错误的 rest-client 上传文件

ruby-on-rails - 如何将 image_tag 与远程 URL 一起使用?

ruby-on-rails - 如果条件为真,则用回形针应用处理器

ruby-on-rails - 数据库迁移脚本中断引用

ruby-on-rails - "Or"语句在 Rails 查询中不起作用

java - 玩! 2+ 以 RESTfull 方式添加 JSON 支持

ruby-on-rails-3 - main :Object 的未定义方法 `send_data'

ruby-on-rails - 如何使用sidekiq worker访问Dokku服务器上rails应用程序中上传的文件

ruby-on-rails - rails : Sending Emails with Dynamic Delivery Options is not working

ruby-on-rails - Rails 回形针 `is not recognized by the ' 识别'命令`