7

I just noticed this error and I need to upgrade my Rails 3.1 project (I'm not using asset pipeline) to jQuery 1.7+.

I see that there are already jQuery libraries in my javascripts folder (public/javascripts). Short of copying the new jQuery library into there manually, is there a comment to replace the JavaScript file using Ruby?

Community
  • 1
  • 1
Homan
  • 25,618
  • 22
  • 70
  • 107
  • 1
    What's wrong with copying a new version of the jQuery library into your `public/javascripts` directory? That's what I do when I want to upgrade one of my javascript's in a rails project. – Batkins Dec 28 '11 at 22:43
  • I know next to nothing about Rails, but if possible, try and figure out a way to externally connect to jQuery's [jquery-latest.js](http://code.jquery.com/jquery-latest.js) file. That contains the latest revision of jQuery available. – Zack Zatkin-Gold Dec 28 '11 at 23:37

3 Answers3

13

In Rails 3.1, jQuery is managed by the jquery-rails gem. You can upgrade your jQuery version by using a newer version of jquery-rails. It's very easy to do. Here's a full explanation.

See your existing version by running gem list from project root directory. You'll probably see something like this:

...
i18n (0.6.0)
jquery-rails (1.0.16, 1.0.14, 1.0.13)
json (1.6.1)
...

The jquery-rails gem uses jQuery 1.7+ in versions 1.0.17+. As of this writing, the latest version of the gem for Rails 3.1 is 1.0.19, which uses jQuery 1.7.1. That sounds like what you want!

So you don't need to drop anything in your /javascripts folder. Instead, specify the newer version of the gem in your Gemfile. Here's what I have in mine:

gem "jquery-rails", "~>1.0.19"

The funny ~> character tells bundler to find a version of the gem that is at least what you specify (1.0.19 here), and any later minor releases, but not the next major release (which is 2.0.0 for this gem, supporting only Rails 3.2+).

Then, from the project root, run bundle and the specified version will be set up for you. Restart your Rails app, reload the page, and you should be able to verify that you are now dealing with jQuery 1.7.1.

Let me know how it goes!

Cheers.

brookr
  • 1,504
  • 12
  • 14
  • 2
    `bundle show jquery-rails` is a faster way to check which version of the gem you have installed. – Simon Polak Jul 19 '13 at 01:42
  • This does not work for me at all. I was running 2.0.1 and changed it to 2.1.0, uninstalled the old gem and installed the new one. jQuery is still being loaded at 1.8 when it should be 1.10. – Aristata Dec 11 '13 at 23:10
3

In Rails 4 You can Update jquery with following way

in application.js manifest

//= require jquery2 //for jquery 2

//= require jquery3 //for jquery 3
uzaif
  • 3,511
  • 2
  • 21
  • 33
0

I updated my jquery-rails gem version to version 3.1.2 (type: bundle show jquery-rails to see your current jquery-rails version, or gem list to see all gems)

However my webpage was still using the outdated jquery version 1.3. To check what version you are using, go to your jquery.js file which resides in your assets/javascripts folder, the version should be specified there. Of course in order to use jquery you also have to include jquery.js in your assets/javascripts/application.js file as well, by adding the following line:

//= require jquery

And in the case you also have other JS libraries like prototype for example, you will definitely want to check out this link as well: jQuery- Avoiding Conflicts with Other Libraries

Anyway back to the point, as per zzg's recommendation above, I opened the link that he supplied: Latest Jquery and literally copied and pasted the content of that webpage into my jquery.js file. After I restarted the server and refreshed my webpage, my jquery was using the latest version. My jQuery was working locally on my dev env, but wasn't working on production immediately after (trying to figure that out right now).

Lastly, one more thing that might be useful to know, is that you can always see what version your rails app is using (in your local dev env) by right clicking on your rails app web page and choosing "View Page Source", you should find a link like this:

<script data-turbolinks-track="false" src="/assets/jquery.js?body=1"></script>

Clicking on it will show the content of your jquery.js file which specifies the version (this will not work on production since asset pipeline compresses all javascript files into one file, namely application.js)

AmitF
  • 1,287
  • 13
  • 9