7

我最近更新了Rails 3.1。在生產(Heroku)上的Rails 3.1中顯示爲空白的圖像

這裏就是我加了一部分:

<%= asset_path('logo_symbol.png') %> 

這使得/assets/logo_symbol.png其中工程開發環境完全沒有問題。但是,當我將代碼推到heroku上時,它會顯示一張破碎的圖像,其URL爲:assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png。我猜測這個新名字是出於某種優化的原因。

然而,有趣的是,當我轉到生產網址時,我看到一個空白頁面,但是當我將該網址更改爲任意隨機數時,例如向其中添加數字時,它會顯示找不到頁面。很顯然,它在這個網址上找到了一些東西。當我直接在生產/ heroku上訪問/assets/logo_symbol.png時,它也顯示一個空白頁面。

如果這有幫助,當我推送代碼時,heroku不會成功預編譯,而且heroku的文檔說目前沒有解決該問題的方法。

在此任何幫助將不勝感激。

我的猜測是它與某些與環境有關的配置有關。我附上我的application.rb中,development.rb的內容和production.rb文件

這裏是我production.rb文件

# Settings specified here will take precedence over those in config/application.rb 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the webserver when you make code changes. 
    config.cache_classes = false 

    # Log error messages when you accidentally call methods on nil. 
    config.whiny_nils = true 

    # Show full error reports and disable caching 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    # Don't care if the mailer can't send 

    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    config.action_mailer.delivery_method = :smtp 

    # Print deprecation notices to the Rails logger 
    config.active_support.deprecation = :log 

    # Only use best-standards-support built into browsers 
    config.action_dispatch.best_standards_support = :builtin 

    # Do not compress assets 
    config.assets.compress = false 

    # Expands the lines which load the assets 
    config.assets.debug = true 
end 

module ActiveAdmin 
    class Reloader 
    def attach! 
    end 
    end 
end 

的內容,這裏是我development.rb文件的內容

# Settings specified here will take precedence over those in config/application.rb 

    # In the development environment your application's code is reloaded on 
    # every request. This slows down response time but is perfect for development 
    # since you don't have to restart the webserver when you make code changes. 
    config.cache_classes = false 

    # Log error messages when you accidentally call methods on nil. 
    config.whiny_nils = true 

    # Show full error reports and disable caching 
    config.consider_all_requests_local  = true 
    config.action_controller.perform_caching = false 

    # Don't care if the mailer can't send 

    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    #config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
    config.action_mailer.delivery_method = :smtp 

    # Print deprecation notices to the Rails logger 
    config.active_support.deprecation = :log 

    # Only use best-standards-support built into browsers 
    config.action_dispatch.best_standards_support = :builtin 

    # Do not compress assets 
    config.assets.compress = false 

    # Expands the lines which load the assets 
    config.assets.debug = true 
end 

module ActiveAdmin 
    class Reloader 
    def attach! 
    end 
    end 
end 

這裏是我的production.rb文件的內容

# Settings specified here will take precedence over those in config/application.rb 

    # The production environment is meant for finished, "live" apps. 
    # Code is not reloaded between requests 
    config.cache_classes = true 

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

    # Specifies the header that your server uses for sending files 
    config.action_dispatch.x_sendfile_header = "X-Sendfile" 

    # For nginx: 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 

    # If you have no front-end server that supports something like X-Sendfile, 
    # just comment this out and Rails will serve the files 

    # See everything in the log (default is :info) 
    # config.log_level = :debug 

    # Use a different logger for distributed setups 
    # config.logger = SyslogLogger.new 

    # Use a different cache store in production 
    # config.cache_store = :mem_cache_store 

    # Disable Rails's static asset server 
    # In production, Apache or nginx will already do this 
    config.serve_static_assets = false 

    # Enable serving of images, stylesheets, and javascripts from an asset server 
    # config.action_controller.asset_host = "http://assets.example.com" 

    # Disable delivery errors, bad email addresses will be ignored 
    # config.action_mailer.raise_delivery_errors = false 

    #config.action_mailer.default_url_options = { :host => 'ha1.heroku.com' } 
    config.action_mailer.delivery_method = :smtp 

    # Enable threaded mode 
    # config.threadsafe! 

    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 
    # the I18n.default_locale when a translation can not be found) 
    config.i18n.fallbacks = true 

    # Send deprecation notices to registered listeners 
    config.active_support.deprecation = :notify 

    # Compress JavaScripts and CSS 
    #config.assets.compress = true 

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

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

    # Defaults to Rails.root.join("public/assets") 
    # config.assets.manifest = YOUR_PATH 

    config.assets.js_compressor = :uglifier 
    config.assets.css_compressor = :scss 

我已經將我的配置文件與3.1版的rails文檔進行了比較,並且似乎具有所需的所有默認設置。不過,我仍然沒有看到任何形象。任何幫助將非常感激

回答

13

從production.rb刪除此行:

 
config.action_dispatch.x_sendfile_header = "X-Sendfile" 

你也應該對齊你的配置文件的設置與那些在section 9 of the pipeline guides

Sendfile標頭包含上游網絡服務器的信息,以便在哪裏查找文件(在文件系統上)以便爲其提供服務。這將從後端(Rails/Sprockets)中刪除負載。當sendfile處於HTTP響應中時,它不包含任何內容(它的長度爲零),這就是爲什麼你什麼都看不到的東西。

在heroku上,nginx服務器無法訪問應用程序文件系統,所以這不起作用。

請參閱this note關於Heroku dev站點重新發送文件。

如果您使用的是heroku,this document概述了有效使用管道的最佳選擇。

+0

我刪除了該行,並確保鏈接頁面上提到的行出現在我的文件中。我在heroku直播網站上仍然有相同的問題,它不顯示圖像。任何想法爲什麼? – alik

+1

嗯實際上,劃傷,它只是工作。我認爲這是緩存早期 – alik

+0

你是對的,我的問題是緩存。清除我的瀏覽器緩存後,它工作正常。 – Ryan

0

你需要做兩件事來解決它。 首先,在production.rb文件中將這兩行從false更改爲true。

 config.assets.compile = true 
     config.assets.digest = true 

其次,如果你的語法像這樣爲你的形象

background: url("imgo.jpg") 

將其更改爲

 background: image-url("image.jpg") 

我希望它做你的工作。