2015-09-15 22 views
4

在解決我的項目中的圖片問題時遇到問題。Rails 4,Heroku上的回形針不能識別破碎的圖像

總結:Rilas 4託管在Heroku使用Paerclip與S3

問題開始與具有使用以前使用的自定義上傳邏輯與S3。圖片網址看起來像這樣/profile_picture/:style_:image_hash。它適用於那裏的圖像,但圖像不存在的回形針仍然試圖訪問不存在的圖像,實際的鏈接看起來像這樣:http://s3.amazonaws.com/project/profile_pictures/100h_

has_attached_file :picture, 
       styles:   { :'53h' => '', :'100h' => '' }, 
       convert_options: { 
        :'100h' => '-gravity center -thumbnail 165x165^ -extent 165x165', 
        :'53h' => '-gravity center -thumbnail 45x45^ -extent 45x45' 
       }, 
       path:   'profile_pictures/:style_:filename', 
       default_url:  '/images/default-pp-large.jpg' 

我猜測,可能是因爲實際文件名中的風格,但我不知道,eather方式defauly_url不能正常工作和圖像全部被破壞,不包括實際上存在的人。

你能幫忙嗎?

回答

3

最後,我做了一個猴子pach回形針寶石。添加了此行config/initializers/paperclip.rb

module Paperclip 
    class Attachment 
    alias_method :original_url, :url 

    def url(style_name = default_style, options = {}) 
     if @instance.public_send("#{@name.to_s}_file_name").blank? 
     'default-pp-large.jpg' 
     else 
     original_url(style_name, options) 
     end 
    end 
    end 
end 
1

我想知道ID如何不存在於圖片的路徑中,如在這種情況下,如果2個不同圖片具有相同的名稱,它會檢索第一個匹配,我認爲,路徑應該是這樣的:

path: 'profile_pictures/:id_:style_:filename' 
# OR 
path: 'profile_pictures/:id/:style_:filename' 

不知道是否應該完全解決問題,但這是它的一部分。

+0

我不能這樣做,因爲,就像我說的,遷移與現有的S3文件來了,所以改變實際路徑文件是不是一種選擇。 – user2945241

相關問題