【问题标题】:How to the use the image_tag with a remote URL?如何将 image_tag 与远程 URL 一起使用?
【发布时间】:2012-07-28 10:16:08
【问题描述】:

我有一个 rake 任务,它将我从 API 缓存的图像上传到我的 S3 存储桶。在我看来,我尝试输出图像,但它似乎不起作用。我想要做的是将图像缓存到我的文件系统中,将它们发送到 S3,我想使用我的 S3 存储桶而不是我的文件系统中的图像位置。我的代码如下所示:

在我的 rails 控制台中,我这样做只是为了检查图像 url:

1.9.3p125 :002 > a.image
 => http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629 
1.9.3p125 :003 > 

我在我的应用程序中使用 Paperclip,是否应该将 url 添加为“http:://”?显得比较诡异。我的 index.html.erb 中的代码如下所示:

<li>
  <%= movie.title %>
  <%= image_tag movie.image.url %>
</li>

但这会导致以下html:

<li>
  Cowboy Bebop
  <img alt="1" src="/assets/http:://s3-eu-west-1.amazonaws.com/ramen-hut/pictures/1.jpg?1343645629">
</li>

为什么它在我的网址前包含“/assets”/?

我按照教程配置了 Paperclip 以设置我的欧洲 S3 存储桶的图像 url。所以在我的 environment.rb 中,我有这个:

#Signature correction for Paperclip and AWS
AWS::S3::DEFAULT_HOST = "s3-eu-west-1.amazonaws.com"

我的初始化器目录中有一个 aws-signature.rb 文件,其中包含以下代码:

#Makes Paperclip use the correct URL for images
Paperclip.interpolates(:s3_eu_url) { |attachment, style|
  "#{attachment.s3_protocol}://s3-eu-west-1.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
}

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    URL 有问题:http::// 而不是 http://,因此 image_tag 不知道它是绝对 URL。

    您如何生成这些 URL? Gem 还是您自己的代码?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多