简体   繁体   中英

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.

I have done a search for application.js file and it is only included in my assets folder not inside my public folder. I only have one copy of each js file as well so it should probably not be due to the assets pipeline.

This is my application.js file.

//= 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. 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.

//= 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/). 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.

Alternatively, if you want to specifically white-list the files that should be loaded, then remove //= require_tree . from your application.js file.

//= require_tree .

This line will load all the files in your application.js file's folder. 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.

Here in your case, Removing require_tree line should solve your problem. I hope it helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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