Rails如何在特定的erb视图中包含scss文件?(Rails How to include a scss file in a specific erb view? [duplicate])

编程入门 行业动态 更新时间:2024-10-27 22:23:16
Rails如何在特定的erb视图中包含scss文件?(Rails How to include a scss file in a specific erb view? [duplicate])

这个问题在这里已有答案:

宝石更新后:测试失败,“资产未被声明为在生产中预编译” 3个答案

我想在view/work/index.html.erb包含assets/stylesheets/work.scss文件

我查了这个问题

如何在布局之外的erb中包含css或javascript?

并在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 ?

This question already has an answer here:

After gem update: test fail with “Asset was not declared to be precompiled in production” 3 answers

I want to include the assets/stylesheets/work.scss file in view/work/index.html.erb

I've check this question

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

and add this in layout/application.html.erb

<head> ... <%= yield(:header) if content_for? :header%> </head>

Then, add this in index.html.erb

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

However, it threw this error

I was doing this in development mode instead of production, why I need 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是一个很好的参考。

Because you are including it independently, and not including it in your application.css. The default for the asset pipeline is to compile applicaiton.css/application.js and anything that is included in those files.

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

If you want it to be seperate and not compiled into the one monolithic file, being called from your stylesheet_link_tag, you have to manually add it to the precompile array.

http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets is a good reference.

更多推荐

本文发布于:2023-08-01 20:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   文件   在特定   scss   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!