您可以创建一个不同的布局文件,仅包含您需要的资产。我通常面临的一个典型场景是网站的管理部分,其中布局从公共部分发生变化。
在这种情况下,您可以创建这样的布局views/layouts/admin.htm.erb
:
<!DOCTYPE html>
<html>
<head>
<title>Admin</title>
<%= stylesheet_link_tag "admin", :media => "all" %>
<%= javascript_include_tag "admin" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
如您所见,js 和 css 资产有两个不同的清单文件。这意味着有两个文件:
/assets/javascripts/admin.js
/assets/stylesheets/admin.css
就像在中一样,application.js
您application.css
可能需要引导程序和该特定布局可能需要的其他资产。
这里有一个例子:
/*
* 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 vendor/assets/stylesheets of plugins, if any, 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 top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require bootstrap
*= require_self
*/
最后一件事情
您需要告诉 rails 预编译您创建的新清单文件。
在/config/environments/production.rb
# Precompile additional assets (application.js.erb, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
config.assets.precompile += %w( admin.css admin.js )
现在您可以使用布局
在您的控制器中:
layout "admin"