在生产(Heroku)上的Rails 3.1中显示为空白的图像 [英] Image showing as blank in Rails 3.1 on Production (Heroku)

查看:89
本文介绍了在生产(Heroku)上的Rails 3.1中显示为空白的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了Rails 3.1。



以下是我添加的一部分:

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

这会呈现 /assets/logo_symbol.png 在开发环境中工作得很好。但是,当我将代码推送到heroku的生产环境时,它会显示一张破碎的图像,其URL为: assets / logo_symbol-135ddc8db2c9b59f032bed7db520137a.png 。我猜测这个新名字是出于某种优化的原因。



不过值得注意的是,当我转到 assets / logo_symbol-135ddc8db2c9b59f032bed7db520137a.png 生成网址时,我看到一个空白页面,但是当我将该网址更改为随机的任何内容时,如向其中添加数字,它会显示未找到的页面。很显然,它在这个网址上找到了一些东西。它也显示了一个空白页面,当我直接在production / heroku上访问 /assets/logo_symbol.png 时。



如果这有任何帮助,那么在我推送代码时,heroku不会成功进行预编译,而且heroku的文档显示目前没有针对该问题的解决方法。



任何在这方面的帮助将不胜感激。



我的猜测是它与某些与环境有关的配置有关。我附加了我的application.rb,development.rb和production.rb文件的内容。

这里是我的production.rb文件的内容。

 #这里指定的设置将优先于config / application.rb中的设置

#在开发环境中,应用程序的代码被重新加载在
#每个请求。这会减慢响应时间,但对于开发
#来说是完美的,因为当您更改代码时不必重新启动Web服务器。
config.cache_classes = false

#当您意外地调用nil方法时记录错误消息。
config.whiny_nils = true

#显示完整的错误报告并禁用缓存
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

#不关心邮件程序是否无法发送

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

#打印弃用通知给Rails记录器
config.active_support.deprecation =:log

#仅使用内置于浏览器中的最佳标准支持
config.action_dispatch.best_standards_support =:内置

#不压缩资产
config.assets.compress = false

#扩展加载资产的行
config.assets.debug = true
结束

模块ActiveAdmin
类Reloader
def附加!
end
end
end

以下是内容我的development.rb文件

 #这里指定的设置优先于config / application.rb中的设置

#在开发环境中,您的应用程序的代码将重新加载到
#的每个请求中。这会减慢响应时间,但对于开发
#来说是完美的,因为当您更改代码时不必重新启动Web服务器。
config.cache_classes = false

#当您意外地调用nil方法时记录错误消息。
config.whiny_nils = true

#显示完整的错误报告并禁用缓存
config.consider_all_requests_local = true
config.action_controller.perform_caching = false

#不关心邮件程序是否无法发送

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

#打印弃用通知给Rails记录器
config.active_support.deprecation =:log

#仅使用内置于浏览器中的最佳标准支持
config.action_dispatch.best_standards_support =:内置

#不压缩资产
config.assets.compress = false

#扩展加载资产的行
config.assets.debug = true
结束

模块ActiveAdmin
类Reloader
def附加!
end
end
end

这里是我的内容production.rb文件

 #这里指定的设置优先于config / application.rb中的设置

#生产环境适用于完成的实时应用程序。
#代码不会在请求之间重新加载$ ​​b $ b config.cache_classes = true

#完全错误报告被禁用并且缓存处于打开状态
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

#指定您的服务器用于发送文件的标头
config.action_dispatch.x_sendfile_header =X-Sendfile

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

#如果您没有支持X-Sendfile之类的前端服务器,则
#只是注释掉,Rails会提供这些文件

#查看日志中的所有内容(默认为:info)
#config.log_level =:debug

#在分布式设置中使用不同的记录器
#config.logger = SyslogLogger.new

#在产品中使用不同的缓存存储
#config.cache_store =:mem_cache_store

#禁用Rails的静态屁股et服务器
#在生产中,Apache或nginx将已经执行此操作
config.serve_static_assets = false

#从资产服务器启用图像,样式表和JavaScripts服务
#config.action_controller.asset_host =http://assets.example.com

#禁用投递错误,不良邮件地址将被忽略
#config.action_mailer.raise_delivery_errors = false

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

#启用线程模式
#config.threadsafe!

#启用I18n的语言环境回退(查找任何语言环境回落到
#无法找到翻译时的I18n.default_locale)
config.i18n.fallbacks = true

#将弃用声明发送给已注册的侦听器
config.active_support.deprecation =:notify

#压缩JavaScript和CSS
#config.assets。 compress = true

#如果预编译的资源不存在,则不要回退到资产管道
config.assets.compile = false

#为资产URL生成摘要
config.assets.digest = true

#默认为Rails.root.join(public / assets)
#config.assets.manifest = YOUR_PATH

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

我已将我的配置文件与3.1的rails文档进行了比较,看起来好像我拥有所有需要的默认设置。不过,我仍然没有看到任何形象。任何帮助将不胜感激

解决方案

从production.rb中删除此行:

 
config.action_dispatch.x_sendfile_header =X-Sendfile

您还应该调整设置在您的配置文件中使用管道指南的第9部分

Sendfile标头包含上游网络服务器的信息,以便在哪里查找文件(在文件系统上)以便为其提供服务。这将从后端(Rails / Sprockets)中删除负载。当sendfile处于启用状态时,HTTP响应中不包含任何内容(长度为零),这就是为什么你什么也看不到的情况。



在heroku上,nginx服务器无法访问应用程序文件系统,所以这是行不通的。



请参阅本笔记 Heroku的dev站点重新发送文件。



如果您使用的是heroku,本文档概述了有效使用管道的最佳选择。


I recently updated my Rails to 3.1.

Here's a part where I added:

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

This renders /assets/logo_symbol.png which works perfectly fine in development environment. However, when i push the code to production on heroku, it shows a broken image, with the url: assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png. I am guessing the new name is for the reason of some optimization.

It is however interesting to note that when I go to the assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png url on production, I see a blank page, but when I change that url to anything random, like adding numbers to it, it shows a page not found. So clearly it is finding something on that url. It also shows a blank page when I go to /assets/logo_symbol.png directly on production/heroku.

If this is any help, heroku does not precompile successfully when I push the code and heroku's documentation says that there is currently no work around for that issue.

Any help in this will be greatly appreciated.

My guess is that it has something to do with some configuration related to environments. I am attaching contents of my application.rb, development.rb and production.rb files

here are the contents of my production.rb file

 # 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

and here are the contents of my development.rb file

 # 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

Here are the contents of my production.rb file

  # 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

I have compared my config files with the rails documentation for 3.1 and seems like I have all the defaults needed. However I am still seeing no image. Any help will be much appreciated

解决方案

Remove this line from production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

You should also align the settings in your config files with those in section 9 of the pipeline guides.

Sendfile headers contain information for the upstream webserver of where to find the file (on the file system) to serve it. This removes the load from the backend (Rails/Sprockets). When sendfile is on the HTTP response contains no body (it is zero length) which is why you see nothing.

On heroku the nginx servers do not have access to the application filesystem, so this won't work.

See this note on the Heroku dev site re sendfile.

If you are using heroku, this document outlines the best options for using the pipeline effectively.

这篇关于在生产(Heroku)上的Rails 3.1中显示为空白的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆