Rails coffee script throws error
I use rails 3.1.0rc4 and coffee-script 2.2.0. app/assets/javascripts/application.js includes:
//= require users
In app/assets/javascripts/users.js I have following code:
jQuery(document).ready(function($) {
alert('OK')开发者_如何学Python;
});
How to convert it to coffee-script? When I replace:
jQuery(document).ready
with
$->
and change the filename from users.js to users.js.coffee
it throws ExecJS::RuntimeError.
The answer to your problem is simple: The input
$->
causes the CoffeeScript compiler to complain
Error: Parse error on line 1: Unexpected '->'
You need to either add a space:
$ -> alert 'OK'
or use explicit parentheses:
$(-> alert 'OK')
jQuery(document).ready ($) ->
alert('OK')
精彩评论