4

My Rails 3.1.rc4 app was working fine, but I'm trying to figure out the appropriate way to store my js files in the pipeline. If I put any code in a file other than application.js, I get the following error:

Started GET "/assets/application.js" for 127.0.0.1 at 2011-07-21 23:15:02 -0500 Compiled ~/Dropbox/Rails/myapp/app/assets/javascripts/application.js.coffee (224ms) (pid 69397) Error compiling asset application.js: ExecJS::ProgramError: SyntaxError: Reserved word "function" on line 1 (in /Users/micahalcorn/Dropbox/Rails/myapp/app/assets/javascripts/users/registrations.js.coffee) Served asset /application.js - 500 Internal Server Error

This happens regardless of which file (registrations in this case) and claims a 'reserved word' regardless of the first word in the file (var, function, etc). I am using node.js as my runtime environment, and everything works fine if I remove coffeescript compiling gems and treat it like a Rails 3.0 app. I want to better understand the asset pipeline and follow conventions. Thanks for any suggestions!

Micah Alcorn
  • 2,363
  • 2
  • 22
  • 45

1 Answers1

8

The error

SyntaxError: Reserved word "function" on line 1

is a CoffeeScript compiler error. Either convert function to -> in registrations.js.coffee, or rename it to registrations.js so that the file will be read as raw JavaScript.

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • I thought that I could mix raw js in with cs. Simple enough, thanks! – Micah Alcorn Jul 22 '11 at 16:05
  • 2
    You can *kinda* mix raw JS in with CoffeeScript (by putting backticks around it), but it's usually not a good idea to do that. Instead, keep your JS in `.js` files and your CoffeeScript in `.js.coffee` files. Sprockets makes it easy to let the two languages coexist. – Trevor Burnham Jul 22 '11 at 17:12
  • I don't understand. According to the CoffeeScript site: "You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa)." – Nowaker Jan 29 '12 at 22:37
  • 2
    @DamianNowak Yes, but you can't mix JavaScript and CoffeeScript code in the same file. What the docs are saying is that CoffeeScript files can use objects/functions defined in JavaScript files, and vice versa. – Trevor Burnham Jan 29 '12 at 23:34