3

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

4

2 回答 2

1

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

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

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

    *= 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

于 2013-01-14T17:45:17.520 回答
-2

在您的alpha.rb设置config.assets.compiletrue预编译资产

于 2013-01-14T17:39:24.343 回答