Rails如何在特定的erb视图中包含scss文件?

时间:2016-06-02 03:41:13

标签: css ruby-on-rails sass erb

我想在assets/stylesheets/work.scss

中加入view/work/index.html.erb文件

我已经查看了这个问题

How to include a css or javascript in an erb that is outside the layout?

并在layout/application.html.erb

中添加此内容
<head>
...
<%= yield(:header) if content_for? :header%>
</head>

然后,在index.html.erb

中添加
<% content_for :header do -%>
<%= stylesheet_link_tag 'work' %>
<% end -%>

然而,它抛出了这个错误 enter image description here

我是以development模式而不是production执行此操作,为什么我需要precompile

1 个答案:

答案 0 :(得分:0)

因为您是独立包含它,而不是将其包含在application.css中。资产管道的默认值是编译applicaiton.css/application.js以及这些文件中包含的任何内容。

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any styles
 * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
 * file per style scope.
 *
 *= require_tree .
 *= require c3
 *= require purecss
 *= require_self
 *= require pure_css_reset
 */

如果您希望它是单独的而不是编译成一个整体文件,从stylesheet_link_tag调用,则必须手动将其添加到预编译数组中。

http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets是一个很好的参考。