Rails 4 - 生产图像和字体不指向指纹

时间:2017-04-15 17:36:21

标签: ruby-on-rails asset-pipeline

我将自定义主题导入到主页的Rails应用程序中。我已将整个文件夹复制到app/assets/customlibrary中,这样我就不必拆分主题文件了。

当我在生产环境中运行我的应用程序时,Rails在app/assets/customlibrary/*中被引用时无法找到指纹资产 app/assets/customlibrary/css/style.css

例如,在style.css中,background: url("../images/hero-image.jpg")会产生GET http://localhost:3000/assets/images/hero-image.jpg 404 (Not Found)

我无法弄清楚如何解决这个问题。有什么建议吗?

文件夹结构

app/assets/customlibary/css/*
app/assets/customlibary/css/style.css
app/assets/customlibary/js/*
app/assets/customlibary/fonts/*.(svg|eot|woff|tff|)
app/assets/customlibary/images/*
app/assets/customlibary/images/hero-image.jpg

app/assets/stylesheets/themes/theme.css'
app/assets/stylesheets/style1.css'
app/assets/stylesheets/style2.css'

application.rb中

config.active_support.escape_html_entities_in_json = true
config.filter_parameters += [:password]
config.encoding = "utf-8"

# setup bower components folder for lookup
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components')
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components', 'bootstrap-sass-official', 'assets', 'fonts')
config.assets.paths << Rails.root.join('app', 'assets', 'customlibrary')

# customlibrary assets
config.assets.precompile += %w( css/* fonts/* images/* js/* )
# normal stuff
config.assets.precompile << /\.(?:svg|eot|woff|ttf)$/
config.assets.precompile << /\.(?:png|jpg)$/
config.assets.precompile += %w( base.css )
config.assets.precompile += ['themes/theme.css']
config.assets.precompile += ['style1.css', style2.css', ... ]

production.rb

config.cache_classes = true

config.eager_load = true

config.consider_all_requests_local       = false
config.action_controller.perform_caching = true

config.serve_static_assets = true

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

config.assets.compile = false

config.assets.digest = true

config.assets.version = '1.0'

config.log_level = :debug

1 个答案:

答案 0 :(得分:1)

config.assets.paths中添加路径并不意味着它可以从应用程序的Web根目录中获取资源。 (这是my_app/public

它为Sprockets和资产助手使用的查找路径添加了一个目录。从样式表链接到资源时,您可以将扩展名更改为.css.erb并使用插值:

.class { background-image: url(<%= image_path 'image.png' %>) }

如果您使用的是SASS,sass-rails会将Rails资产助手映射到SASS功能,以便您可以使用:

.class { background-image: image-url("image.png") }

这不需要您重命名文件。