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!
This happened to me about a week ago here is my solutions. First I made sure my application.js file had this at the beginning of the file.
//= require jquery
//= require jquery_ujs
//= require_self
//= require_tree .
Another thing is make sure rails is not using the public directory. Sometimes you will have both public and assets but make sure you are telling rails to use assets. Rails 3.2 should automatically include that though. Open your config>>application.rb and check for the following.
# Enable the asset pipeline
config.assets.enabled = true
config.assets.version = '1.0'
if article
represents an element id or class, you need to select it with .article
or #article
. i forget that all the time.
I've fixed the same problem by deleting all files in %RAILS_ROOT/lib/assets/ folder.