为什么在Rails 3.1的HAML布局中,javascript_include_tag :defaults无法工作?

9

伙计,这些东西都是什么鬼啊。你知道Rails 3.1中那行代码的实际作用吗?

<script src="/assets/defaults.js" type="text/javascript"></script>

正如ESPN所说的:“来吧,伙计。”

我知道资产不再被视为二等公民。但在这个发行候选版中,它们似乎甚至无法获得绿卡。在新的app/assets/javascripts/application.js中:

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

所以,我应该下载jQuery吗?我该怎么做?因为那个javascript目录里除了application.js什么都没有。

很烦人。但是它是免费的,所以我在抱怨什么?无论如何,这些问题似乎相当基础,但我会感激您提供的任何帮助。

1个回答

17
在Rails 3.1中,不再有所谓的“defaults”,而是在application.js文件中指定的内容成为“defaults”。您可以使用以下代码行来包含此文件:
 javascript_include_tag "application"
jquery-rails 宝石包中包含 jqueryjquery_ujs 文件,该宝石包在默认的 Rails 3.1 Gemfile 中。

该文件中的 //= require 行告诉 Sprockets 你想要引用一个文件,这种情况下会是从 jquery-rails 引用 jquery.js,而 //= require_tree . 则会将 application.js 目录中的所有其他 JavaScript文件都引用并合并成一个文件。

你可以在此处了解有关资产管道的更多信息


2
谢谢RB。这让我疯了。我猜nifty-scaffold还没有跟上。我简直不敢相信JS文件被嵌入到gem中。这对我来说有点太“神奇”了。我无法弄清楚Sprockets如何知道在那个gem中查找,而且我不喜欢它。 - RubyRedGrapefruit
由于提供的链接已经失效,这里提供了一个关于Rails资产管道的文档链接:http://guides.rubyonrails.org/asset_pipeline.html - t3__rry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接