Rails 3 Javascript Not Working [英] Rails 3 Javascript Not Working

查看:221
本文介绍了Rails 3 Javascript Not Working的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Rails 3.2应用程序中包含了<%= javascript_include_tag应用程序%> 并且,我在company.js.coffee文件中包括以下CoffeeScript,该文件在运行时包含在最终的application.js文件中:

  $(article h2 a)。click(e) - > 
$(article div)。hide()
$(@ hash).slideToggle()
e.preventDefault 1))。hide()

然而,脚本根本不工作在从它复制的原始HTML文件(虽然不是在CoffeeScript)。类似地,AJAX表单请求似乎也不起作用。



有人知道为什么会发生这种情况吗?有没有什么我缺少?

解决方案

我终于找到了答案(我应该先试过的东西)。我假设当其包括资产文件夹中的其他文件时,rails会自动包括application.js文件中的 $ - > 开启程序。但是,似乎你需要为每一个。所以,我只是添加了 $ - > 之前的一切,它现在工作。



同样的问题,请确保 $ 选择器没有被任何其他函数使用,否则你需要做完整的CoffeeScript JQuery函数参考。 p>

感谢所有的帮助!


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!

这篇关于Rails 3 Javascript Not Working的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆