I have included Javascript with <%= javascript_include_tag "application" %> in my Rails 3.2 application. And, I have included the following CoffeeScript in the company.js.coffee file that is included in the final application.js file when run:
$("article h2 a").click (e) ->
$("article div").hide()
$(@hash).slideToggle()
e.preventDefault()
$("article div:not(#1)").hide()
However, the script doesn’t work at all, despite functioning in the original HTML file that it was copied from (albeit not in CoffeeScript). And, similarly, the AJAX form requests don’t seem to be working either.
Does anyone know why this may be occurring? Is there something I’m missing?
I finally found the answer (something I should have tried first). I assumed that rails was automatically including the
$ ->opener in the application.js file when it included the other files in the asset folder. But it seems that you need to do it for each one. So, I just added$ ->before everything and it works now.And a note for others reading with the same problem, be sure that the
$selector isn’t being used by any other functions, else you’ll need to do the full CoffeeScript JQuery function reference.Thanks for all of the help!