0

大约一周以来,我一直在处理生产中的 Rails 资产管道的复杂性。我终于快完成了。我在 css 文件中引用了两个图像。

正在预编译 css 文件,并且在文件中相应地引用了预编译的图像。

background:url(/assets/k-opacity-70-f75f0169dbfb178b3aedbf11429dfd68.png);

#intro{background:#111 url(/assets/intro-bg-12afabffede338ee9a72dbff2488bfea.jpg) no-repeat center;

我已经根据 Rails 网站上的建议编辑了我的 config/application.rb,以确保我正在预编译我需要的所有文件。

 config.assets.precompile << Proc.new do |path|
  if path =~ /\.(css|js)\z/
    full_path = Rails.application.assets.resolve(path).to_path
    app_assets_path = Rails.root.join('app', 'assets').to_path
    if full_path.starts_with? app_assets_path
      puts "including asset: " + full_path
      true
    else
      puts "excluding asset: " + full_path
      false
    end
  else
    false
  end
end

除了确保资产已预编译之外,我还需要做些什么吗?我的应用程序正在部署到带有 nginx 和乘客的数字海洋服务器。

4

1 回答 1

1

我认为你的问题在这里:

background: url("<%= asset_path 'k-opacity-70.png' %>"); position: fixed; top: 0; left: 0; }

帮手

有一个名为的助手asset_url,强烈推荐用于 CSS。我会这样做:

#app/assets/stylesheets/application.css.scss #-> notice the extension
background: asset_url("k-opacity-70.png");

这将确保您的资产被正确引用,尤其是当您将它们发送到您的服务器时。这听起来可能很简单,但我们之前在 Heroku 中遇到过问题(我知道你使用 DO),文件没有呈现,因为它们的指纹不正确

可能会因此而被否决(因为它与您的代码基本相同),但这对我们有用


指纹识别

其次,我会确保您的指纹图像确实存在于您的public/assets文件夹中

预编译过程通常会创建一系列images, stylesheets&js文件,这些文件必须在您的资产中引用。这是由我上面提到的助手处理的,但这也意味着它们可能被错误引用

--

如果您的文件夹中有正确的文件,问题将是您如何引用它们,否则它将是预编译过程本身的问题(在 的职权范围内application.css

于 2014-05-19T09:19:04.510 回答