2016-03-23 60 views
1

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

如果我检查了生产中的HTML(从JSON建)根页上的代码是:

1日的JBuilder:<img src="https://example.com/small_thumb_default.png">

2ST的JBuilder:<img src="https://example.com/assets/small_thumb_default.png">

这些都不是工作。

在此之上,如果我去,让我们给用户的网页说,然后它会尝试获取照片喜欢:

1日的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 

回答

0

最有可能的,有问题的文件是内部app/assets/文件夹,该文件夹内的文件正在默认预编译并在f的末尾添加一个随机摘要ilename。为了使所描述的行为起作用,small_thumb_default.png文件应放置在public/文件夹内,因此它不会在预编译时附加摘要。无论是或者你应该避免使用HTML <img>并使用Rails <%= image_tag %>,这样你就可以使用预编译文件名来查看渲染。