Rails JQuery Is Loaded Twice Problem

Do you like this?

Recently I ran into a problem in which jQuery file was loaded twice. The first one was included in the jquery.js file and the other is included in the application.js.


I was trying to extend jQuery using jQuery.fn.extend feature but the new methods were not available. Particularly, after loading the page, I used javascript debugger in Firefox and found that $().myMethod was unavailable. Then I debugged step by step into the file in which I declared the new features and found they were available at that time until application.js got its turn to be executed. I opened the file and the content was exactly the same as the jQuery library:

 * jQuery JavaScript Library v1.4.2
 * http://jquery.com/
 * Copyright 2010, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 * Date: Sat Feb 13 22:33:48 2010 -0500
After hours of checking, I found the problem was that when I downloaded the source code from the server back to my laptop, the public/assets folder which included the pre-compiled javascript files was also there. Rails gives priority to the files in the public folder. Hence, the compiled application.js which included all the compressed javascript from the server contained the jQuery library. I removed the files from there and it worked.

comments powered by Disqus