Rails3.1 - 如何在某些视图中包含css文件而不包含其他视图?

Not*_*Dan 8 ruby-on-rails-3 compass-sass

看来,在rails 3.1中,所有css.scss文件都合并为1个文件.如果我想将css文件仅包含在某些视图中,该怎么办?就像我想要管理页面中包含admin.css.scss和home/about/contact页面中的main.css.scss一样.

dam*_*brz 13

在Rails 3.1中,如果application.css如下所示,所有样式表将合并到application.css中:

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree . 
*/
Run Code Online (Sandbox Code Playgroud)

这是由于*= require_tree.

您可以要求具有以下特定样式表:

*= require main
Run Code Online (Sandbox Code Playgroud)

否则,在您的布局中,您写道:

%head
 = yield :head
Run Code Online (Sandbox Code Playgroud)

并在您的页面中:

= content_for :head do
 = stylesheet_link_tag 'stylesheet'
Run Code Online (Sandbox Code Playgroud)


Ely*_*Ely 10

让我添加一个对我有用的解决方案.

如前一个答案中所述,您可能希望删除

 *= require_tree . 
Run Code Online (Sandbox Code Playgroud)

application.css文件中的语句.

我保留了

 *= require_self
Run Code Online (Sandbox Code Playgroud)

整个应用程序的共享样式的声明.

然后在我的application.html文件中,我使用以下语句在视图中仅包含application.csscontroller.controller_name.css样式表.

= stylesheet_link_tag "application", controller.controller_name
= javascript_include_tag "application", controller.controller_name
Run Code Online (Sandbox Code Playgroud)

正如您可以看到JavaScript文件的相同作品.