rake资产:预编译不起作用(rails 3.1.1)

Nic*_*nto 7 windows rake assets heroku ruby-on-rails-3

我正在部署到heroku但我看到没有提供css文件(它们也无法在heroku上找到).

我读到我需要做rake资产:首先在本地预编译然后当我这样做时,我得到:

C:\project>bundle exec rake assets:precompile --trace

** Invoke assets:precompile (first_time)
** Execute assets:precompile
rake aborted!
undefined: Unexpected token: operator (<)
  (in C:/project/app/assets/javascripts/application.js)

Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)

我在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 .
Run Code Online (Sandbox Code Playgroud)

谢谢

更新

如果删除.js.erb文件,我会收到以下错误

C:\project>bundle exec rake assets:precompile RAILS_ENV=production --trace
** Invoke assets:precompile (first_time)
** Execute assets:precompile
rake aborted!
706: unexpected token at 'C:\Users\me\AppData\Local\Temp\execjs20111021-6448-ei2nm3.js(2, 3) Microsoft JScript runtime error: Out of memory

'
  (in C:/project/app/assets/javascripts/application.js)

Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)

仍然有erb css和js文件无法编译的问题...

这似乎没有结束..谢谢

Mik*_*ike 5

我一直在努力尝试部署到临时服务器.适合我的解决方案是确保您的config/environments/[your_environment].rb文件中包含以下内容:

config.assets.compress = false
Run Code Online (Sandbox Code Playgroud)

默认情况下,压缩器在生产以外的环境中不可用,这就是预编译失败的原因.


Car*_*sin 5

我在这里有同样的问题!在我的情况下,导致此问题的原因是,我向javascript文件夹添加了一个新的JS文件,undefined: Unexpected token: operator (<)当我尝试运行预编译命令时出错.所以我查看了新的js文件,发现该文件中有一个HTML样式注释<!-- -->.我删除它,生活现在好!

因此,请尝试找出<!-- -->js文件中是否存在任何HTML样式注释,并删除这些注释.从html文件中复制一些JS代码时尤其如此!


Siw*_*申思维 3

我认为这是由外部 javascript 文件引起的,该文件的代码格式不正确。例如

function say_hi(){
  // NOTICE that there's no semi-colon there!
  name = "Jim"
  alert("hi" + name )
}
Run Code Online (Sandbox Code Playgroud)

在预编译下,您的代码将放入 1 行,并且由于没有必要的分号,因此您生成的 js 文件可能包含错误,例如

"undefined: Unexpected token: operator (<)" 
Run Code Online (Sandbox Code Playgroud)

或者其他的东西。

所以,我的建议是:

  1. 如果代码格式不好,请不要压缩 js 文件,方法是按照 @Mike 的答案在配置文件中设置“config.assets.compress = false”。

  2. 尽可能使用coffeescript,它将帮助您生成格式非常好的代码。(我不是coffeescript大师,所以如果我错了请纠正我)