2012-08-06 71 views
4

我的生產環境 -的Rails 3.2 asset_host設置忽略

# Code is not reloaded between requests 
    config.cache_classes = true 
    config.assets.enabled = true 

    # Full error reports are disabled and caching is turned on 
    config.consider_all_requests_local  = false 
    config.action_controller.perform_caching = true 

    # Disable Rails's static asset server (Apache or nginx will already do this) 
    config.serve_static_assets = true 
    config.static_cache_control = "public, max-age=31536000" 

    # Compress JavaScripts and CSS 
    config.assets.compress = true 
    config.assets.js_compressor = :uglifier 
    #config.assets.css_compressor = :yui 

    # Don't fallback to assets pipeline if a precompiled asset is missed 
    config.assets.compile = true 

    # Generate digests for assets URLs 
    config.assets.digest = true 

    # See everything in the log (default is :info) 
    config.log_level = :warn 
    config.log_tags = [:remote_ip, lambda { |req| Time.now }] 

    # Enable serving of images, stylesheets, and JavaScripts from an asset server 
    ActionController::Base.asset_host = Proc.new { |source| 
    unless source.starts_with?('/stylesheets' || '/javascripts') 
     "//dddd.cloudfront.net/" 
    end 
    } 

然而,當使用IMAGE_TAG它仍然返回我「/資產..」相對URL而不是絕對的URL到資產主我。

irb(main):034:0> helper.image_tag('noimage.gif') 
=> "<img alt=\"Noimage\" src=\"/assets/noimage.gif\" />" 
irb(main):035:0> helper.image_path('noimage.gif') 

我似乎無法確定可能會丟失什麼。我甚至嘗試過簡單的config.asset_host設置,但它仍然無法識別設置。

回答

3

您是否嘗試過設置指定的配置?

config.action_controller.asset_host = 'https://abcd.cloudfront.net' 

不確定它是否適用於協議相關網址。我只是在我的應用程序中使用https。

這也可能是值得注意的是行動郵件也有類似的設置:

config.action_mailer.asset_host = 'https://abcd.cloudfront.net' 
+0

是的,我嘗試沒有協議相對網址以及。它也沒有工作。 – Shekhar 2012-08-06 18:23:25

+0

我的配置文件出現問題,導致無法正常工作。將其剝離到最低限度後 - asset_host似乎可以與https://前綴一起使用。所以它不適用於協議相對URL,它具有完整的URL。謝謝! – Shekhar 2012-08-06 20:37:07

+3

@Shekhar什麼是阻止工作的問題?我有同樣的問題。謝謝! – 2013-05-14 19:14:17

-1

有一個奇怪的現象在Rails的配置。根據文檔asset_host應該只包含host部分('yoursite.com')。我決定用一個幫手:

module EmailsHelper 

    def email_image_tag(src) 
    protocol = ActionMailer::Base.default_url_options[:protocol] || 'http' 
    image_tag asset_path(src, protocol: protocol) 
    end 

end 

配置它在你的郵件:

class EmailNotificationsMailer < ActionMailer::Base 

    add_template_helper(EmailsHelper) 
    ... 

end 

有沒有辦法強迫IMAGE_TAG使用其它的URL,而不是一個URL傳遞給它。