简体   繁体   English

Rails jQuery被两次调用

[英]Rails jQuery being called twice

I am working on a Rails 3.2 application but somehow my javascript codes gets executed twice and I am not sure why. 我正在使用Rails 3.2应用程序,但是不知何故我的javascript代码被执行了两次,我不确定为什么。

I have done a search for application.js file and it is only included in my assets folder not inside my public folder. 我已经搜索了application.js文件,它仅包含在我的资产文件夹中,而不包含在公用文件夹中。 I only have one copy of each js file as well so it should probably not be due to the assets pipeline. 每个js文件也只有一个副本,因此它可能不应归因于资产管道。

This is my application.js file. 这是我的application.js文件。

//= require jquery
//= require jquery_ujs
//= require jquery_nested_form
//= require jquery.ui.all
//= require myownjs
//= require_tree .

I have actually done an almost similar project without jquery nested form and the problem does not exist so my gut tells me that it might be due to the gems I am using calling my codes repeatedly or due to the order of my javascript including but I am new to web programming and I am not sure if that actually makes sense. 我实际上做了一个几乎没有jquery嵌套形式的几乎类似的项目,并且这个问题不存在,所以我的直觉告诉我,这可能是由于我正在使用gem反复调用我的代码,或者是由于我的javascript顺序,包括但我是Web编程的新手,我不确定这是否真的有意义。 However, I did try removing that require line for nested forms and it did not make a difference. 但是,我确实尝试删除了嵌套表格的要求行,但没有任何区别。

Could someone help me out by suggesting what the error might be? 有人可以提示错误可能会帮助我吗?

This is the application.js for the other project. 这是另一个项目的application.js。

//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require myowncodes

//= require_tree .

//= require_tree . already includes everything in the . 已包含中的所有内容. directory - which will be whatever directory your application.js file resides in (typicaly /app/assets/javascripts/). 目录-这将是您的application.js文件所在的目录(通常为/ app / assets / javascripts /)。 My guess is that many of the other files you have listed are already in that directory and those entries can be removed from the application.js file. 我的猜测是,您列出的许多其他文件已经在该目录中,并且这些条目可以从application.js文件中删除。

Alternatively, if you want to specifically white-list the files that should be loaded, then remove //= require_tree . 或者,如果要专门将应加载的文件列入白名单,请删除//= require_tree . from your application.js file. 从您的application.js文件中。

//= require_tree .

This line will load all the files in your application.js file's folder. 此行将加载application.js文件文件夹中的所有文件。 To have full control on the files being loaded, you can remove the above line and load individual required files or you can make sub folders and use 要完全控制要加载的文件,可以删除上面的行并加载单个所需的文件,也可以制作子文件夹并使用

//= require_directory <sub_directory_name>

This will load all the files in that folder. 这将加载该文件夹中的所有文件。 This approach will load only the files specified by you. 此方法将仅加载您指定的文件。 I suggest not to use require_tree, to have better control on the files that you are loading with your application. 我建议不要使用require_tree,以便更好地控制要随应用程序加载的文件。

Here in your case, Removing require_tree line should solve your problem. 在这种情况下,删除require_tree行应该可以解决您的问题。 I hope it helps!! 希望对您有所帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM