【问题标题】:Rails assets not precompiling, css looks different in productionRails 资产未预编译,css 在生产中看起来不同
【发布时间】:2014-05-30 11:57:09
【问题描述】:

我的 rails 应用程序在开发模式下可以正常工作并且看起来完全符合我的要求,但在生产中它在 chrome 和 safari 上看起来不同,在 safari 中加载徽标图像而不是字体,在 chrome 中加载字体而不是图像加上输入字段在 chrome 中稍长且未对齐,但在开发模式下,在 chrome 中看起来都很棒

我已经搞砸了一段时间,并删除了几次公共/资产

rake assets:precompile RAILS_ENV=production 

没有成功,预编译通过,没有错误

config/application.rb:

 # Settings in config/environments/* take precedence over those specified here.
 # Application configuration should go into files in config/initializers
 # -- all .rb files in that directory are automatically loaded.
 config.assets.paths << "#{Rails.root}/assets/fonts"
 config.assets.paths << "#{Rails.root}/assets/images"
 config.assets.paths << Rails.root.join("app", "assets", "fonts")
 config.assets.precompile += %w( .svg .eot .woff .ttf )


# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.assets.enabled = true  
#config.assets.paths << "#{Rails.root}/app/assets/fonts" 

配置/环境/生产:

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

# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true

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

# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx,  varnish or squid.
# config.action_dispatch.rack_cache = true

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
#config.assets.compile = true

config.assets.precompile =  ['*.js', '*.css', '*.css.erb', '*.css.scss'] 
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
config.assets.paths << "#{Rails.root}/assets/fonts"
config.assets.paths << "#{Rails.root}/assets/images"
config.assets.precompile += %w( .svg .eot .woff .ttf )
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/
# Generate digests for assets URLs.
config.assets.digest = true

# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'

【问题讨论】:

  • 你的生产环境是什么?您要部署到 Heroku 吗?
  • 不,我在 puma 上运行的 Nitrous.IO 上使用我自己的盒子
  • 你也在运行 nginx 吗?您可能需要在生产配置中将 config.serve_static_assets 设置为 false。您也可以发布您的徽标和 CSS 链接代码吗?
  • 没有运行nginx,如果我在production.rb和application.rb中有重复的设置有关系吗?
  • 欢迎在这里查看我的配置文件:jamestansley.com/2013/06/23/…。自从我部署到 Heroku 后,一些设置会有所不同,但请查看我的预编译设置。还要确保在布局中使用正确的 CSS 样式表链接

标签: css ruby-on-rails ruby-on-rails-4 assets


【解决方案1】:

在您的 config/environments/production.rb 文件中,设置:

config.serve_static_assets = false(目前设置为真)

config.assets.compile = true(目前设置为false)

这应该可以解决您的问题。

让我解释一下我要你做什么。

  • 通过设置config.serve_static_assets = false,我们告诉rails服务器,不要添加ActionDispatch::Static中间件,用于服务静态资产。

为什么不呢?

这是因为在生产环境中,您需要在 Web 服务器(如 Apache/Nginx)后面运行应用程序服务器(如 puma),该服务器旨在直接提供静态文件(包括资产),而无需费心发送向 Rails 应用服务器发出请求。

由于您没有使用任何网络服务器,我们将其关闭。

  • 通过设置config.assets.compile = true,我们告诉rails 在运行时编译请求的资源。即在app/assetsvendor/assetslib/assets 中查找请求的资产,如果从这些位置中的任何一个位置找到,则提供它。

默认情况下,config.assets.compile 在开发环境中为true,在生产环境中为false。由于我们没有使用 Web 服务器来提供静态资产,因此我们要求 Rails 实时编译我们的资产。

更多详情请参阅asset pipeline documentation

【讨论】:

  • config.assets.compile = true 对我来说是最重要的。有时,与设置一起使用的 cmets 并不能真正阐明它的影响。这更像是告诉 rails 期望编译的资产,而不是真正让 rails 本身 应该编译它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 2013-05-08
  • 2013-05-28
  • 2016-10-26
  • 1970-01-01
相关资源
最近更新 更多