small medium large xlarge

Back to: All Forums  CoffeeScript
07 May 2011, 11:27
Bharat Ruparel (148 posts)

Hello Trevor, The book is looking good! I think you should expand on the coverage of Coffeescript with jQuery in Rails 3.1. A small but real world example would go a long way. Thanks. Bharat

19 Jul 2011, 01:56
Ma Bo (4 posts)

Highly agreed. With the release of Rails 3.1, both CoffeeScript and jQuery are supported by default, which means Rails developers will write a lot jQuery code in CoffeeScript, though there’re several blogs talked about it, we still need an exemplary reference.

20 Sep 2011, 19:09
Trevor Burnham (54 posts)

I’ve already talked to my editor about doing a book update to cover Rails 3.1 and running Node on Windows (now that it’s possible to do so without Cygwin). But you’ll have to wait for at least a little while. If you just want a short example:

Create a Rails 3.1 project and generate a controller. You’ll get a file automatically (its name will correspond to that of the controller), and Sprockets will automatically pull it in and compile it (via the require_tree directive in application.js). Note that despite the filename, your new script will run everywhere in your app (by default), not just on pages corresponding to that controller. So if you make the file, say,

$ -> alert 'Hello, Rails 3.1!'

and then refresh any page in your app (that includes the default application.js), you’ll get an annoying pop-up greeting.

The most common issue people bump into is that each CoffeeScript file lives in a different scope. Remember that if you want a variable from one CoffeeScript file to be visible from another, you have to make it a global by writing

window.x = ...

or, for short,

@x = ...

(since the scripts are run in the @window@ context).

I’ve been taking a lot of questions about Rails 3.1 + CoffeeScript + jQuery over at Stack Overflow, so you might want to try searching for anything else you want to know there.

You must be logged in to comment