pipeline 是 rails3.1 后引进的一个很有用的特性,它可以:
以 js 文件举例子,在 app/asset/javascript 中有一个 application.js 文件,它的内容如下:
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require sequence.jquery-min.js
//= require_tree .
很简单,你可以将app/assets/javascripts
, lib/assets/javascripts
, vendor/assets/javascripts
想象成一个逻辑目录,然后 rails 的 pipeline 引擎会到逻辑目录里去找这个文件中提到的 js 文件,然后 compile 到 application.js 里面去。 但是有个例外,在上面的例子中,require_tree 不会到逻辑目录里去找,而是只到'app/assets/javascripts'里去找。所以即使你用了requre_tree .
, 你还是需要手动将 lib 和 vendor 里需要的 js 文件包含进来。
The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file. You can also use the require_directory directive which includes all JavaScript files only in the directory specified, without recursion.
希望能帮到一些人,不会像我一样错误的理解一个知识点,浪费 2 个小时找 js 文件为什么没有。 BTW:如果喜欢,请点击喜欢按钮。。。