CSS需求订单 - 资产管道

时间:2016-08-29 20:40:07

标签: ruby-on-rails asset-pipeline

我有一个应用程序呈现名为feeds的视图,它附带了自己的样式feeds.scss。该视图使用名为default_app.scss的布局。

控制器操作有render layout: 'default_app'。 在default_app.html.erb中,= stylesheet_link_tag 'default_app', params[:controller]表示首先需要布局样式表。

这里的问题是在default_app.scss内,我定义了一组常量颜色,例如:$red: #aaa但是当我在$red中使用feeds.scss时,它是未定义的。这没有意义,因为不应该在页面之前需要布局样式(查看特定的css)?

app
- assets
  - stylesheets
    - newsfeeds
      - feeds.scss (want to use colors here)
  - default_app.scss (defined colors here)
- controllers
  - newsfeeds_controller.rb
- views
  - newsfeeds
    - feeds.html.haml
  - layouts
    - default_app.html.haml (= stylesheet_link_tag 'default_app', params[:controller])

2 个答案:

答案 0 :(得分:1)

Scss variables are processed at compile time, the order you use them doesn't actually matter for variables since, by the time the code links to the .css file, the variables were already replaces by the scss compiler on the assets pipeline, feeds.css.scss doesn't know what $red means since it's defined in another file.

You have to include the variables on every .scss file that uses them.

_colors.css.scss

$red: #ff0000;
$blue: #0000ff;
$gree: #00ff00;

default_app.css.scss

@import('colors');
.something {
  color: $red;
}

feeds.css.scss

@import('colors');
.other_thing {
  color: $red;
  background: $blue;
}

答案 1 :(得分:0)

这是我的第一枪。 我不确定你对params[:controller]做了什么。 stylesheet_link_tag将源作为参数,请参见此处 http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/stylesheet_link_tag

这是默认设置 - stylesheet_link_tag "style", media: "all"

如果您正在寻找控制器特定示例 - 请参阅此处的示例 http://guides.rubyonrails.org/asset_pipeline.html

“您还可以选择仅使用以下内容在各自的控制器中包含控制器特定的样式表和JavaScript文件:”

<%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>

将第2.4节(http://guides.rubyonrails.org/asset_pipeline.html)读取到您通过application.css处理主要布局css的位置。这应该指向正确的方向。这可能是由于您需要文件的方式。如果您仍然卡住,请随时显示您需要的文件

相关问题