【问题标题】:Rails 3.2 precompressing selected assetsRails 3.2 预压缩选定资产
【发布时间】:2012-11-28 16:50:13
【问题描述】:

所以,我的<head> 中有以下代码行,在app/views/layouts/application.html.erb 内:

<%= stylesheet_link_tag "front", :media => "all" %>

这在开发中工作得很好,但是当我切换到生产时它会因为它没有预编译而中断,所以我运行rake assets:precompile,它将一堆文件添加到我的公共目录中,也很好,但是所有的 css 和 js 文件是这样的名字:

  • application-digest.ext
  • application-digest.ext.gz
  • application.ext
  • application.ext.gz

其中extcssjs,而digest 明显不同。

图像文件仅带有 name.extname-digest.ext 版本。

因此,当导航到该页面时,我收到一个 ActionView::Template::Error 告诉我 front.css isn't precompiled

由于我对不同的控制器有不同的布局(FrontController 使用 application.html.erb 布局,如果这很重要的话),我不想包含 所有的 css 或 js 文件在所有控制器布局中,因为这只是对带宽的可耻浪费。

我如何告诉 rails 编译里面的所有文件 app/assets 单独?


我设法通过在config/environments/production.rb 中设置config.assets.compile = true 来使用实时编译“修复”问题,但我一直在到处阅读这不是处理资产的最佳方式[1],因为浪费很多资源。

该应用托管在 Heroku 上。


[1]:official Ruby on Rails guide page:“此模式使用更多内存,性能比默认差,不推荐。”

【问题讨论】:

    标签: ruby-on-rails heroku ruby-on-rails-3.2


    【解决方案1】:

    如果您在本地预编译资产,则需要 git commit 资产,以便 heroku 加载它们。但这不是你想做的。

    /config/environments/production.rb

    config.assets.precompile += %w(front.css, another.css, heres_a_javascript.js)
    

    application.js 和 application.css 会自动预编译。您在 application.html(或任何地方,真的)中链接的所有其他内容都需要在此处预编译

    【讨论】:

    • 谢谢!我前段时间自己找到了答案,正准备发布答案,但你打败了我! :D
    猜你喜欢
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多