【问题标题】:Rails AssetNotPrecompiledError even though the assets are being precompiledRails AssetNotPrecompiledError 即使资产正在预编译
【发布时间】:2013-01-14 16:22:41
【问题描述】:

我是 Ruby 和 Rails 的新手,我已经考虑了好几天了,还没有找到解决方案。我的项目在我的开发环境中运行良好,但在我的 alpha 或生产环境中无法运行。

我的 app/assets/stylesheets 文件夹中有以下样式表:

app
--> assets
  --> stylesheets
    --> modules
      --> _header.css.scss
      --> _footer.css.scss
    --> home.css.scss
    --> user.css.scss
    --> help.css.scss

当我尝试使用我的 alpha 环境(即通过运行 rails s --environment=alpha)时,我收到此错误:

Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError in Home#index
Showing /app/views/home/index.haml where line #1 raised:
home.css isn't precompiled

我正在使用 stylesheet_link_tag('home') 将该样式表添加到我的视图中。

我的alpha.rb 文件有这个:

config.assets.compile = false
config.assets.precompile += %w( help.css home.css users.css )
config.assets.compress = true
config.assets.debug = false

我正在运行预编译任务(即rake assets:precompile RAILS_ENV=alpha)。该任务似乎工作正常。这是输出:

rake assets:precompile RAILS_ENV=alpha --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
/ruby-1.9.3-p286/bin/rake assets:precompile:all RAILS_ENV=alpha RAILS_GROUPS=assets --trace
** Invoke assets:precompile:all (first_time)
** Execute assets:precompile:all
** Invoke assets:precompile:primary (first_time)
** Invoke assets:environment (first_time)
** Execute assets:environment
** Invoke environment (first_time)
** Invoke rails_admin:disable_initializer (first_time)
** Execute rails_admin:disable_initializer
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
** Execute environment
** Invoke tmp:cache:clear (first_time)
** Execute tmp:cache:clear
** Execute assets:precompile:primary

我的文件确实以/public/assets/ 结尾,/public/assets/manifest.yml 正在创建中,我的文件也列在其中:

application.css: application.css
help.css: help.css
home.css: home.css
users.css: users.css

如果我尝试访问服务器(通过转到http://localhost:3000),我会收到前面提到的AssetNotPrecompiledError 错误home.css;但是,http://localhost:3000/assets/home.css 确实返回了样式表。

AFAIK 生成此错误是因为 Rails 不知道 home.css 的位置;但是,它在manifest.yml 上列出,我认为它应该在那里寻找它。

如果您需要更多信息,我很乐意提供。这让我发疯了!

-- 编辑--

根据 Qumara SixOneTour 的要求,这是我的 application.css.scss:

@import 'modules/header';
@import 'modules/footer';

body {
    background: $bgColor asset-url('bgFull.jpg', image) repeat-x center top;
}

div.main {
    padding: 35px 30px;
    background: $whitweColor;
    margin-top: -3px;
    padding-bottom: 30px;
    @include border-bottom-left-radius(5px);
    @include border-bottom-right-radius(5px);

    h1 {
        font-size: 2em;
    }

    h2 {
        font-size: 1.5em;
    }
}

@media only screen and (max-width: 767px) {
  body {
        background: $bgColor asset-url('bg.png', image) repeat-x;
    }
}

基本上,我不使用清单,而是在每个视图中加载单独的 css 文件。

【问题讨论】:

  • 你能发application.css文件吗?
  • 我将其添加到问题中。提前致谢!
  • 你有没有弄明白这件事?我在使用新的 Rails 3.1.11 应用时遇到了完全相同的问题,我什至得到了图像的 AssetNotPrecompiledError(位于 /app/assets/images 并预编译到 /public/assets)。
  • 对不起@ilasno,我从来没有这样做过......这样的事情让我停止使用 Rails。我现在使用 Node.js 和 Express 的工作效率更高了。

标签: ruby-on-rails ruby-on-rails-3.2 asset-pipeline


【解决方案1】:

我建议您坚持使用清单文件的默认设置。它们是资产管道的管理点。在你的情况下,有几件事要做:

  • 将任何 css 定义从 application.css 移动到 app/assets/stylesheets 中的单独 css 文件

  • 以“清单”样式包含您的部分 - 这是一个奇怪的部分,您可以告诉行已注释,但它们没有。像这样:

    *= require_self  
    *= require jquery 
    *= require jquery_ujs
    *= require_tree .
    
  • 如果您使用新目录扩展assets/stylesheets,就像您正在尝试做的那样,最好在application.rb 中提及这一点:

    config.assets.paths << Rails.root.join("app", "assets", "styleshhets", "modules")
    

最后一个建议:在开发环境中删除您的public/assets。它使事情变得无用复杂。只需依靠app/assets

【讨论】:

  • 我不想使用application.css作为manifest,因为有些CSS规则如果全部加载会相互冲突。我更愿意为每个 HTML 文档加载一个单独的 CSS/JS 文件;我确信 Rails 有办法做到这一点,因为有一种方法可以将资产添加到 config.assets.precompile。资产正在预编译 AFAIK,只是 rails 不知道它们。我试过手动设置 config.assets.manifest,但没有骰子:(
  • 是否可以根据相关的控制器分隔 .js(或 .css)文件?
  • 现在我认为它会做,但我可能需要更精细的东西,比如每个视图不同的 js 或 css 文件。
【解决方案2】:

在您的 alpha.rb 中将 config.assets.compile 设置为 true 以预编译资产

【讨论】:

  • 这在性能方面是不可行的。加载几个静态 css 和 js 文件需要太多不必要的开销。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2011-12-20
  • 2014-07-12
  • 1970-01-01
相关资源
最近更新 更多