snowguy
snowguy

Reputation: 911

how does require jquery work in Rails 3 app?

In my rails 3 app it has:

//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery-ui

And it works fine. But I don't understand how it works. I think I understand the third line which I believe adds everything to this file that is found in the same directory as this file (/app/assets/javascript). But what about the first line? Where does it get the jquery file? I don't see it in any of the directories it mentions in the comment at the beginning of the file, specifically:

// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.

Where does rails go to get the jquery javascript file?

Upvotes: 9

Views: 4683

Answers (3)

TonyTakeshi
TonyTakeshi

Reputation: 5929

The require part you mentioned above is called asset pipeline, which is part of new features of rails 3. The goal of this is to concatenate all javascripts file together, so you have your page load faster by a single import of the javascript file.

You can find out more about asset pipeline here and if not mistaken it is utilizing sprockets gem.

Refering to //= require jquery, it is importing the javascript file from your jquery gem (only if you using jQuery gem). You can find it from your jQuery gem assets folder.

Refer this screencast as it described best.

Hope it helps.

Upvotes: 6

Pedro Nascimento
Pedro Nascimento

Reputation: 13886

The comment can be a little confusing. A gem is also called a "plugin" in this case. It gets from the jquery-rails gem. Note the vendor/assets/javascripts structure.

https://github.com/rails/jquery-rails/tree/master/vendor/assets/javascripts

Upvotes: 1

Zabba
Zabba

Reputation: 65467

require_tree . includes all files under the directory it is in (e.g. app/assets/javascript). The jQuery source file come from the jquery-rails gem

Upvotes: 2

Related Questions