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

Rails How to include a scss file in a specific erb view?

我想在 view/work/index.html.erb

中包含 assets/stylesheets/work.scss 文件

我已经检查了这个问题

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 -%>

但是,它抛出了这个错误

我是在 development 模式而不是 production 下做的,为什么我需要 precompile

因为您是独立包含它,而不是将其包含在您的 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 是一个很好的参考。