简体   繁体   中英

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');
});

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM