运行 `rake assets:precompile` 时出现错误

3

我有一个Rails 3.1应用程序,我正在尝试将其推送到Heroku。虽然推送成功了,但当它尝试rake资产时出现错误。它会得到一个错误。

Unexpected token punc, expected punc (line: 11225, col: 7, pos: 321149)
undefined
(in /Users/Matt/Orchive/Orchive/app/assets/javascripts/application.js)

然而,这个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

我不是很理解什么是资产,但我在推送之前在终端中尝试运行rake assets:precompile,结果得到了一个大错误。

/Users/Matt/.rvm/rubies/ruby-1.9.2-p180/bin/ruby /Users/Matt/.rvm/gems/ruby-1.9.2-p180/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
rake aborted!
Unexpected token punc, expected punc (line: 11225, col: 7, pos: 321149)

undefined
(in /Users/Matt/Orchive/Orchive/app/assets/javascripts/application.js)

Tasks: TOP => assets:precompile:primary
(See full trace by running task with --trace)
rake aborted!
Command failed with status (1): [/Users/Matt/.rvm/rubies/ruby-1.9.2-p180/bi...]

Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

如果使用--trace会更有帮助的话,我可以编辑这个并将其更改为所显示的内容。我认为这是一个常见且容易修复的错误,只是我不知道该怎么做。


展示在 application.js 中引起错误的那一行可能会有所帮助。 - sosborn
那是奇怪的地方,app/assets/javascript/application.js 文件只包含注释。 - Vasseurth
你在那个目录下还有哪些 JavaScript 文件? - sosborn
2个回答

5

谁说你在使用RefineryCMS? 检查错误和错误原因!您在application.js文件中或包含在application.js文件中的文件中缺少逗号或分号,例如您正在使用require tree(表示将从javascript文件夹中包括每个单独的文件),我猜测某些文件出了问题。 :( - Muhammad Sannan Khalid
同时粘贴此命令的输出“bundle exec rake assets:precompile --trace” - Muhammad Sannan Khalid
1
我删除了application.js文件,现在它可以完美地运行。 - Vasseurth
使用 JavascriptLint 找到了我的代码中的语法错误。 - reto
请至少提供下投票的原因! - Muhammad Sannan Khalid
显示剩余2条评论

3

我的建议是删除 require_tree 这一行。这往往会引起过度包含并创建更多问题。最好是具体地要求文件,而不是添加目录中的所有内容。

http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives

说明

你的逗号或分号放错了位置。很少可能是在jquery或jquery_ujs中,它可能在另一个文件中,但值得注意的是require_tree行:

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

在您的问题中,您提到“但是这个application.js文件只包含一些注释。”那不是注释!实际上,它们是需要这些文件并将它们包含在内的。Rails的魔力,在这种情况下是Sprockets,是您可以编写一个JavaScript文件,并在顶部添加要求,这将包括所有这些文件。这样,您可以包括供应商库,如jQuery,但是您只需使用一个JavaScript文件即可。因此,上面的注释包括jquery,rails jquery帮助程序库,然后require_tree包括javascripts目录中的每个其他文件。在本地计算机上打开您的网站并查看源代码。我敢打赌您有一些意外的其他JavaScript文件。

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