在生产环境(Heroku)上,Rails 3.1中的图片显示为空白

7

我最近将我的Rails升级到了3.1版本。

这里是我添加的一部分:

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

这将呈现/assets/logo_symbol.png,在开发环境中完全正常。然而,当我将代码推送到heroku上的生产环境时,它显示一个损坏的图像,带有url:assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png。我猜新名称是出于某种优化的原因。

但值得注意的是,当我访问生产环境上的assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png url时,我看到一个空白页面,但当我将该url更改为任何随机内容,例如添加数字,它会显示未找到页面。因此,显然它正在该url上找到某些内容。当我直接在production/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

我已经将我的配置文件与Rails 3.1文档进行了比较,似乎我已经拥有了所有必需的默认设置。然而,我仍然看不到图像。任何帮助将不胜感激。

2个回答

13

从production.rb中移除以下行:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

你还应该将配置文件中的设置与 管道指南第9节 中的设置对齐。

Sendfile头包含有关上游Web服务器在哪里找到要提供的文件(在文件系统上)的信息。这会减轻后端(Rails / Sprockets)的负载。当sendfile打开时,HTTP响应不包含正文(长度为零),这就是为什么你看不到任何东西的原因。

在Heroku上,nginx服务器无法访问应用程序文件系统,因此这种方法行不通。

请参见Heroku dev网站上的 此注释 以了解有关sendfile的更多信息。

如果您使用的是Heroku,则 此文档 概述了有效使用管道的最佳选项。


我删除了那一行,并确保链接页面中提到的行存在于我的文件中。在Heroku实时网站上仍然存在相同的问题,图片没有显示出来。有什么想法吗? - alik
1
嗯,其实不用管它了,它刚刚成功了。我想之前可能是缓存的问题。 - alik
你说得对,我的问题是缓存。清除了浏览器缓存后,它就正常工作了。 - Ryan

0
你需要做两件事来解决它。 首先,在production.rb文件中将这两行从false更改为true。
      config.assets.compile = true
      config.assets.digest = true

其次,如果您的图像具有以下语法

    background: url("imgo.jpg") 

将其更改为

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

希望它能完成你的工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接