【问题标题】:rails carrierwave + S3 + json responserailscarrierwave + S3 + json响应
【发布时间】:2016-07-11 01:39:00
【问题描述】:

我有一个使用 carrierwaveS3cloudfront 的 rails4 应用程序。我只有后备图像有问题。当我将 html 响应 (<%= image_tag user.profile.avatar.url(:base_thumb), class: "profile-index-avatar" %>) 与助手一起使用时,一切正常,但无法弄清楚如何使其与 json 响应一起使用。

如果我在根页面上查看生产中的 html(由 json 构建),代码是:

第一个 jbuilder:<img src="https://example.com/small_thumb_default.png">

第二个 jbuilder:<img src="https://example.com/assets/small_thumb_default.png">

这些都不起作用。

最重要的是,如果我去用户页面,那么它会尝试获取如下图片:

第一个 jbuilder:<img src="https://example.com/users/small_thumb_default.png">

第二个 jbuilder:<img src="https://example.com/users/assets/small_thumb_default.png">.

我应该改变什么?

jbuilder 第一版

json.array! @other_notifications do |notification|
  ..
  json.profile_image_url notification.sender.profile.avatar.url(:small_thumb)
  ...
end

jbuilder 第二版

json.array! @other_notifications do |notification|
  ..
  if notification.sender.profile.avatar_url == "default.png"
    json.profile_image_url "assets/small_thumb_default.png"
  else
    json.profile_image_url notification.sender.profile.avatar.url(:small_thumb)
  end
  ...
end

上传者

process :resize_to_fit => [400, 400]

version :base_thumb do
  process resize_to_fill: [85, 85]
end

version :small_thumb, :from_version => :base_thumb do
  process :resize_to_fill => [40, 40]
end

def default_url
  [version_name, "default.png"].compact.join('_')
end

【问题讨论】:

    标签: ruby-on-rails json amazon-s3 carrierwave amazon-cloudfront


    【解决方案1】:

    最有可能的是,有问题的文件位于app/assets/ 文件夹内,该文件夹内的文件默认被预编译,并且随机摘要被添加到文件名的末尾。要使所描述的行为起作用,您的small_thumb_default.png 文件应放在public/ 文件夹中,因此它不会在预编译时附加摘要。要么,要么你应该避免使用 HTML,用 Rails 的方式来做:

    # AVOID THIS
    <img src="small_thumb_default.png"/>
    #THIS IS PREFERABLE
    <%= image_tag 'small_thumb_default.png' %>
    

    这样您将使用预编译的文件名呈现您的视图。

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 2023-03-20
      相关资源
      最近更新 更多