0
我最近爲我的應用程序設置了一個CDN,幾乎一切正常,像我的靜態資產,我的JS和CSS。Rails image_tag不使用CDN資產
但一些對象的資產(圖像)繼續使用我的S3 URL來從,數據,例如:
<%= image_tag(@listing.cover.url(:large))%>
而成,在生產中:
<img src="https://musicjungle.s3.amazonaws.com/listing_covers/5045/large.jpg?1484594254" class="fotorama__img" style="width: 548px; height: 548px; left: 91px; top: 0px;">
而不是用我的CDN。爲了記錄在案,這裏是一片我production.rb的,我設置了CDN:
#CDN settings
config.action_controller.asset_host = "d1bfllp5zjnl7u.cloudfront.net"
正如我所描述的,我的所有其他資產都沒有問題呈現,但那些仍然使用S3。也許這與我有一個附件助手有關?我創建了一個將應用程序上傳到S3的圖片,這裏是代碼:
module Shared
module AttachmentHelper
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def has_attachment(name, options = {})
# generates a string containing the singular model name and the pluralized attachment name.
# Examples: "user_avatars" or "asset_uploads" or "message_previews"
attachment_owner = self.table_name.singularize
attachment_folder = "#{attachment_owner}_#{name.to_s.pluralize}"
# we want to create a path for the upload that looks like:
# message_previews/00/11/22/001122deadbeef/thumbnail.png
attachment_path = "#{attachment_folder}/:id/:style.:extension"
if Rails.env.production?
options[:path] ||= attachment_path
options[:storage] ||= :s3
options[:s3_credentials] ||= {
:bucket => 'bucket-name',
:access_key_id => 'KEY_ID',
:secret_access_key => 'ACCESS_KEY'
}
options[:s3_permissions] ||= 'private'
options[:s3_protocol] ||= 'https'
options[:s3_headers] ||= {
'Cache-Control' => 'max-age=315576000',
'Expires' => 10.years.from_now.httpdate
}
else
# For local Dev/Test envs, use the default filesystem, but separate the environments
# into different folders, so you can delete test files without breaking dev files.
options[:path] ||= ":rails_root/public/system/attachments/#{Rails.env}/#{attachment_path}"
options[:url] ||= "/system/attachments/#{Rails.env}/#{attachment_path}"
end
# pass things off to paperclip.
has_attached_file name, options
end
end
end
end