2009-11-23 71 views
8

我想在我的應用程序中使用default_url,但我用S3存儲圖像。我不知道我需要在那裏放置什麼網址,或者如果我必須創建一個虛假圖像,才能讓所有內容都可用。軌道回形針默認圖像與S3

由於我的圖像總是通過S3渲染,我不知道如果我只是在我的公用文件夾中放入一些默認圖像,它是否會工作。它現在似乎沒有工作 - 我也只有一個圖像,我知道它必須調整大小。我可以手動將調整大小的圖像放在那裏,但我仍然不確定默認使用哪個URL。請幫助:)

回答

22

我在公用文件夾中使用帶有S3的回形針和默認圖像。它工作正常。我DEFAULT_URL聲明是這樣的:

:default_url => '/images/:attachment/missing_:style.png' 

這意味着我的附件名爲頭像,設置與風格小及大,我必須創建並把這些圖像在公共目錄:

  • /圖片/頭像/ missing_small.png
  • /images/avatar/missing_large.png

這是pretty well documented

供您參考,或在情況下,問題真的是別的地方,這裏是我的全部回形針聲明:

has_attached_file :avatar, 
    :styles => { :small => '60x60#', :large => '300x300#' }, :default_style => :large, 
    :storage => :s3, 
    :default_url => '/images/:attachment/missing_:style.png', 
    :path => "users/:id/avatar/:style.:extension", 
    :bucket => "bucket name", 
    :s3_credentials => { 
    :access_key_id => "access key id", 
    :secret_access_key => "secret access key" 
    }, 
    :url => ":s3_alias_url", # These two are only required when you alias S3 - e.g. want to use cdn.example.com rather than s3.amazonaws.com 
    :s3_host_alias => "my.aws.alias" 
+0

並沒有爲我工作。我的所有附件都來自S3,但由於某些原因,default_url通過資產管道傳輸。 – bershika 2014-02-07 02:15:45

+0

是否需要命名爲頭像? – Jwan622 2015-01-26 01:49:01

+0

我並不需要完整的路徑。顯然,default_url只是在images文件夾中查找。 – Jwan622 2015-01-28 21:06:05

4

嘗試了這一點: :default_url => ActionController::Base.helpers.asset_path('missing.png')

+0

這是正確的解決方案,因爲如果您需要修改默認圖像,它將自動消除瀏覽器緩存並下載新的更新圖像。 – Chloe 2017-05-11 00:22:03