9

Starting a new project, I just try to rename my application.js to application.js.coffee.

Rails raise a error : SyntaxError: unexpected IDENTIFIER (in my_path/application.js.coffee)

Application.js is empty, just jquery include :

//= require jquery
//= require jquery_ujs

Any ideas? Thanks

2 Answers 2

27

Comments in coffeescript are written with # so replace with:

#= require jquery
#= require jquery_ujs
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, that works. It was things like that I was skeptical about the use of coffee :) Will put flag answer after time limit. Thanks!
coffeescript is really great, you'll get used to its convention quickly. remind you can convert js to coffee easily using tools like http://js2coffee.org/
7

Why would you want to do that?

It is not recommended to add code to your application.js.

If you need to run a coffee script just create a coffee file in the same folder and it will include it automatically.

From the comments in the file:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//

This file is not your regular .js file, it serves the purpose of serving your javascripts to the asset pipeline, its not a good practice to add code here.

Whatever you need to do it can be done another way...

4 Comments

Can you detail that? Why it's not recomended?
I'm curious as to exactly what downsides there are to renaming and editing this file, too. The official Ruby on Rails guides mentions "application.js.coffee.erb" in its documents, for instance, although maybe that's just a generic name.
im not sure what are the downsides, but why rename it anyway? you can always create a coffe file in the same folder and include it through the application.js through the //= require newcoffefile
@RodrigoZurek Some people like consistency which is why they want to rename application.js to application.coffee since all their other javascripts are .coffee. Your answer incorrectly makes the assumption that the reason is to put code in application.coffee and you assert your own preference without actually identifying a valid technical or architectural reason why renaming application.js should be avoided. If you want to label something as a bad practice you need identify the reasons why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.