简体   繁体   English

Rails和Compass-为每个视图/控制器创建不同CSS的正确方法是什么?

[英]Rails and Compass - What is the right way to create different CSS per view/controller?

I'm using rails with compass . 我正在用罗盘导轨。

When using rails without compass, rails has a css per each controller, which contains things that are only relevant to the views in that controller. 使用不带罗盘的滑轨时,每个控制器的滑轨都有一个CSS,其中包含仅与该控制器中的视图相关的内容。

I couldn't get the same behavior with compass, which resulted in having a big monolithic file instead of separate files per controller. 我无法通过罗盘得到相同的行为,这导致了一个大的整体文件,而不是每个控制器都有单独的文件。

What is the recommended approach to solving that problem? 建议解决该问题的方法是什么?

Rails (since 3.1) does create a css file per controller but it's only for practical code separation, all code assets get compiled into one master file per format (.css, .js..), and this is the best practice 90% of the time. Rails(从3.1开始)确实为每个控制器创建了一个css文件,但这仅用于实际的代码分离,所有代码资产均按每种格式(.css,.js ..)编译成一个主文件,这是90%的最佳实践时间。

If you want to use this approach with SCSS or SASS code, just change the file extension to .css.scss or .css.sass (ie: mycontroller.css.scss) 如果要将此方法与SCSS或SASS代码一起使用,只需将文件扩展名更改为.css.scss或.css.sass(即:mycontroller.css.scss)

If you want to separate the compiled files per controller (you probably don't need it, so i'd advise that you understand the asset pipeline before doing it), you'll have to implement the custom behavior yourself. 如果要按控制器分离编译文件(您可能不需要它,因此建议您在执行之前了解资产管道),则必须自己实现自定义行为。 take a look here http://guides.rubyonrails.org/asset_pipeline.html 在这里看看http://guides.rubyonrails.org/asset_pipeline.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM