javascript_include_tag :defaults 不起作用 [英] javascript_include_tag :defaults does not work

查看:49
本文介绍了javascript_include_tag :defaults 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序迁移到 Rails 3.以下(我在一些地方看到推荐的)不起作用:

I'm migrating an app to Rails 3. The following - which I've seen recommended in a few places - does not work:

<%= javascript_include_tag :defaults %>

就我而言,它扩展为:

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

... 导致 404.据我所知,:defaults 不应该包含一个名为defaults.js"的文件;它应该包括一些基本的东西,比如prototype.js 和application.js.

... which results in a 404. As I understand it, the :defaults is not supposed to include a file called "defaults.js"; it's supposed to include a few essential things like prototype.js and application.js.

请注意,就我而言,以下工作正常.只是如果可能的话,我宁愿使用官方推荐的方式:

Note that in my case, the following works fine. It's just that I'd rather use the official recommended way, if possible:

<%= javascript_include_tag :prototype %>
<%= javascript_include_tag :application %>

我正在使用 ruby​​ 1.9.3 运行 Rails 3.2.8.

I'm running Rails 3.2.8 with ruby 1.9.3.

没有在我的 config/application.rb(以任何形式)中有以下行.为了迁移到 Rails 3,我创建了一个新的 Rails 3 应用程序,并使用该 application.rb 作为起点:

I do not have the following line in my config/application.rb (in any form). To migrate to Rails 3, I created a new Rails 3 application, and used that application.rb as a starting point:

config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)

在 app/assets/javascripts 中,我有:

In app/assets/javascripts, I have:

Util.js
application.js
controls.js
dragdrop.js
effects.js
prototype.js

...以及一堆特定于我的应用程序的东西.

... and a bunch of stuff specific to my application.

推荐答案

从 Rails 3.1 开始,它使用资产管道.这意味着您需要改变对资产的管理.

Since Rails 3.1, it uses assets pipeline. It means you need to change your management of assets.

您有一个 assets/javascripts/application.rb 文件,其中包含如下内容:

You have a assets/javascripts/application.rb file which contains something like this:

//= require jquery
//= require jquery_ujs
//= require_tree .

您似乎在 javascripts 存储库中包含了 jquery、jquery_ujs 和所有其他文件.对于最后一行,您无需在此文件中执行任何其他操作.您只需要在视图中包含应用程序文件,rails 就会管理所有内容:

It seems you include jquery, jquery_ujs and all other files in the javascripts repository. With the last line you don't need to do anything else in this file. You just need to include the application file in your view and rails will manage everything:

<%= javascript_include_tag "application" %>

这与样式表完全相同.

然后在生产环境中,资产(图像、样式表、javascripts)将被编译和缩小以提高效率.

Then in production environment, assets (images, stylesheets, javascripts) will be compiled and minified in order to be more efficient.

我建议您阅读有关此处

希望能帮到你

这篇关于javascript_include_tag :defaults 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆