Ruby on Rails 3.1 资产:预编译和图像

8

我无法在生产模式下使Rails 3.1资源管道预编译工作。它总是在SCSS中引用图像时失败,并显示以下错误:

$ bundle exec rake assets:precompile RAILS_ENV=production 
  rake aborted!
  rails.png isn't precompiled
    (in /home/florian/AssetTest/app/assets/stylesheets/application.css.scss)

但是当我查看public/assets目录时,图片已经存在,因此它已经被预编译了:

  $ ls public/assets | grep rails 
    rails-dd352fc2630e5f9aa5685ef1d7fe5997.png

在这种情况下,SCSS文件只包含一些测试代码:
/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree . 
 */

body {
   background: #ffffff image-url('rails.png') no-repeat 0 center;
}

如果在 SCSS 中不使用 image-url 帮助程序,而是使用 url('/assets/rails.png'),预编译将正常工作,并生成一个 manifest.yml 文件在 public/assets 目录中。
有趣的是:如果我将 SCSS 更改回 image-url('rails.png') 并运行另一个预编译,它仍然可以正常工作(我猜测这是因为图像现在已经在清单文件中列出)。
我在这里做错了什么?我真的不想忽略帮助程序(因为使用它们是正确的方法,对吧?),而且我绝对不想手动创建清单文件...
1个回答

6
我曾经也遇到过同样的问题。这显然是Rails 3.1.0中的一个bug,希望能够很快解决。
无论如何,在`production.rb`中,你可以尝试使用以下代码:
config.assets.compile = true

您可能已将其设置为false,这是正确的设置。但是,如果您在SCSS中使用资产助手,则将其设置为false会导致问题。将该值设置为true可以在使用这些助手时正确地进行编译。
请查看GitHub上的此问题获取更多详细信息。

1
刚按照 Github 的建议更新到了 Rails 3.1.1rc1 版本,现在一切正常 :-) - fwalch
2
这个答案似乎不太好,原因在于:https://dev59.com/CWoy5IYBdhLWcg3wBJkx - Ringo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接