2016-03-23 11 views
1

carrierwaveS3cloudfrontとしたrails4アプリがあります。私は、代替画像にのみ問題があります。私がhtmlのレスポンス(<%= image_tag user.profile.avatar.url(:base_thumb), class: "profile-index-avatar" %>)をヘルパーで使用していると、すべて正常に動作しますが、それをどのようにしてjson応答で動作させるのか理解できません。rails carrierwave + S3 + json response

私はルート・ページ上の生産に(JSONから構築された)HTMLをチェックアウトする場合のコードは次のとおりです。

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

2STの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 

答えて

0

ほとんどの場合に、問題のファイルは、app/assets/フォルダ内にあるそのフォルダ内のファイルがされています既定でプリコンパイルされ、ランダムダイジェストがfの最後に追加されていますilename。あなたのsmall_thumb_default.pngファイルは、public/フォルダの中に置かれるべきであると記述された動作のために、それはダイジェストがプリコンパイルに付加されないようにします。どちらか、またはHTML <img>を使わず、Rails <%= image_tag %>を使用すると、プリコンパイルされたファイル名で表示されます。

関連する問題