“Can’t find variable” error with Rails 3.1 and Coffeescript

By default, every CoffeeScript file is compiled down into a closure. You cannot interact with functions from a different file, unless you export them to a global variable. I’d recommend doing something like this:

On top of every coffeescript file, add a line like

window.Application ||= {}

This will ensure that there’s a global named Application present at all times.

Now, for every function that you’ll have the need to call from another file, define them as

Application.indicator_tag = (el) ->
  ...

and call them using

Application.indicator_tag(params)

Leave a Comment