即使资产正在预编译,Rails AssetNotPrecompiledError也是如此

时间:2013-01-14 16:22:41

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

我是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文件。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-2)

alpha.rbconfig.assets.compiletrue预编译资产

相关问题