Background images in rails production don't work

房东的猫 提交于 2019-12-24 10:54:05

问题


Background images in rails 4(production env) don't work. It seems to me that there is a problem with asset pipeline. When I write in css:

selector{
  background-image: url(image.jpg)
} 

it generates http://myapp.com/assets/image.jpg and it doesn't work. If I change url manually to image.jpg-fingerprint(from public/assets) then everything is okay.

ckeditor also doesn't work.

Here is my production.rb

  config.cache_classes = true
  config.assets.enabled = true
  config.eager_load = true
  config.assets.precompile += Ckeditor.assets
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.assets.js_compressor = :uglifier
  config.assets.compile = false
  config.assets.digest = true
  config.assets.version = '1.0'
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
  config.log_level = :info
  config.log_formatter = ::Logger::Formatter.new

回答1:


You are not using the asset pipeline - you are not using the fingerprinted, cached version of the files. To use the asset pipeline, you need to use the new helpers that point to the fingerprinted, cached version of the files. To do this, either embed erb in your css, or use sass. I'll use sass in my example:

Incorrect (doesn't use the asset pipeline):

.class
  background-image: url('image.jpg')

Correct (uses the asset pipeline):

.class
  background-image: image-url('image.jpg')

Further reading: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets



来源:https://stackoverflow.com/questions/19388189/background-images-in-rails-production-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!