0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

3rd party plugins are recommended to go in vendor/assets/javascripts. The stuff in app/ is what you've built/modified.
So awesome. Thanks for the response. When I add the plug-in to my assets directory, do I then need to include it in my html with <script...plug-in></script> or will it be included through the directory tree? I'm assuming the latter... Thanks again.
It will be included via the directory tree. A nice point from @Dave, you should be putting the js file in vendor/assets/javascripts and then add //= require jquery.sticky in you application.js file. As require_tree . will only include all files from app/assets/js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.