MCP
MCP

Reputation: 4536

Using/including javascript correctly in Rails

I'm my application.js file I have:

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .

Does this mean my app is importing jquery? I'm a little confused about exactly what this is doing. In my gemfile I have gem 'jquery-rails'.

In my view I have the following:

  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <%= render 'layouts/shim' %>

I believe the "javascript_include_tag 'application'" is including the "//= require..." lines, correct?

Now if I want to add a "jquery plugin", specifically hcSticky for the navbar, the instructions say to include <script type="text/javascript" src="jquery.hcsticky.js"></script> "below my <script type="text/javascript" src="jquery.js"></script>. Thing is I'm not explicitly declaring this script anywhere. Am I technically not using jquery then since I don't have the anywhere? The documentation seems to say that the javascript_include_tag will do that for me, but I can't tell exactly what it's doing. Man, so much to learn, so much to figure out. Much respect to everyone who understands all this technology. Thanks for any help.

Upvotes: 0

Views: 109

Answers (1)

manoj
manoj

Reputation: 1675

Is jquery included?

The //= require jquery will include the jquery.js file from the jquery-rails plugin. So you have jquery already included in your html.

If you had doubts in that, please open a page in your application (running in development mode) and view the html source code, there you will see all the javascripts included. (In production mode, all the javascript files will be concated and minified into one single file- application.js for performance reasons.)

How to add a specific new jquery plugin?

Download the js file(jquery.sticky.js). Put it in the /app/assets/js folder. And your //=require_tree . will include all files inside /app/assets/js and its subfolders. So your new plugin will be automatically included.

Upvotes: 1

Related Questions