Ruby on rails 3 Rails3.1-如何在某些视图中包含css文件,而在其他视图中不包含css文件?
在rails 3.1中,所有css.scss文件似乎都合并到一个文件中。如果我希望css文件只包含在某些视图中,该怎么办?就像我希望admin.css.scss包含在admin页面中,main.css.scss包含在home/about/contact页面中一样。另请参见:Ruby on rails 3 Rails3.1-如何在某些视图中包含css文件,而在其他视图中不包含css文件?,ruby-on-rails-3,compass-sass,Ruby On Rails 3,Compass Sass,在rails 3.1中,所有css.scss文件似乎都合并到一个文件中。如果我希望css文件只包含在某些视图中,该怎么办?就像我希望admin.css.scss包含在admin页面中,main.css.scss包含在home/about/contact页面中一样。另请参见: (见第2.2.14节“查找布局”) 不同的控制器可以有不同的布局 e、 g.在app/views/layouts下,可以有application.haml和admin.haml 在app/controllers下,你会有一
(见第2.2.14节“查找布局”) 不同的控制器可以有不同的布局 e、 g.在app/views/layouts下,可以有application.haml和admin.haml 在app/controllers下,你会有一个admin\u controller.rb Rails将尝试查找与控制器同名的布局 您还可以替代此行为并指定要使用的布局,例如:
class ItemsController < ApplicationController
layout "admin"
#...
end
class ItemsController
然后,您将在此新布局的app/stylesheets下创建一个admin.scss文件 在Rails 3.1中,如果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 .
*/
这是由于*=require\u树造成的
您可以要求具有以下内容的特定样式表:
*= require main
否则,在布局中,您将写入:
%head
= yield :head
在您的页面中:
= content_for :head do
= stylesheet_link_tag 'stylesheet'
让我添加一个对我有效的解决方案 如前一个答案中所述,您可能希望删除
*= require_tree .
来自application.css文件的语句
我保留了那本书
*= require_self
用于在应用程序中共享样式的语句
然后,在我的application.html文件中,我使用以下语句仅在视图中包含application.css和controller.controller\u name.css样式表
= stylesheet_link_tag "application", controller.controller_name
= javascript_include_tag "application", controller.controller_name
正如您所看到的,%head和yield:head位对JavaScript文件也同样适用。在Haml中,%head=yield:head将生成HTML标记,并通过yield+1从另一个页面插入代码,以实际指定要查看的节,而不是转储相同的旧链接。谢谢。是的,这是一个looong页面,他们更改了分区号