Ruby on rails require jquery - ruby-on-rails

Does Jquery have to be manually included in rails? Or is it a default? I see in the asset pipeline manifest:
require jquery
require jquery-ujs

It doesn't have to be included, but it will be included unless you remove it. Most applications will benefit from it being present, but if you don't use JavaScript in any of your views, you could always leave it out.

Related

How do I get Filterrific Javascript and Assets to run on Rails 6?

I used the code from demo and the scopes and form work by themselves.
When I added Materialize forms, I noticed that the form and filtering doesn't anymore.
Apparently filterrific doesn't load its javascript library (I loaded Jquery for materialize, which works fine).
From my understanding
//= require filterrific/filterrific-jquery
in assets/javascript/application.js won't work (also tried).
I also get an asset error
Asset `filterrific/filterrific-spinner.gif` was not declared to be precompiled in production.
Declare links to your assets in `app/assets/config/manifest.js`.
The gem seems Rails 6 ready (says the table on github), but there is no documentation on how to make it work with Rails 6 and WebPacker.
Add the following line to app/assets/config/manifest.js
//= link filterrific/filterrific-spinner.gif

which way is faster to add jquery to your project

Which of the following way is faster and best to add jquery file,
add jquery file into project with link like below,
src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js">
download jquery file and then add that local file.
In your gemfile:
#Rails jQuery
gem 'jquery-rails'
In assets/javascriptapplication.js
//= require jquery3
or
//= require jquery2
Using CDN, it will be faster but will be slower for the first time(if not loaded already). Once CDN is loaded, it will always run faster.
But there is a drawback of CDN if the host removes that file that will give raise to error of JQuery not found. To avoid that using gem or downloading is preferred.
Conclusion: both ways have their pros and cons you have to pick one yourself.

Buggy bootstrap 3 navbar dropdowns and collapse

I'm using a bootstrap 3 navbar for a site I'm creating for fun. It looks great but I often run into a bug where the dropdowns won't expand or the collapse button won't work. I'm building it in Rails and added the cdn link to bootstrap.min.js at the bottom of my application.html.erb file.
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
Am I putting it in the wrong place? Thanks
Have you checked your console? Bootstrap requires JQuery, so it might not be working if you haven't included JQuery as well.
However, in a Rails app people usually use the gem bootstrap-sass to incorporate Bootstrap-3. This gem allows you to include bootstrap in the asset pipeline. It's easier to prioritize the order of your assets when they are all kept within the asset pipeline.
I would strongly recommend using the bootstrap-sass docs to include Bootstrap in your asset pipeline rather than including bootstrap via CDN in your html. The documentation has a step-by-step guide that tells you exactly how to include it, and it's also updated for Rails 5.
One of the benefits of Rails is that it allows you to keep your code very organized, so it's good to include Bootstrap in that organization.
Helpful Resources:
bootstrap-sass
integrating-rails-and-bootstrap
Update
With the bootstrap-sass gem your application.js file should look like this (plus any additional javascripts you might have added):
// app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
Most importantly, it needs these two lines in this order to work:
//= require jquery
//= require bootstrap-sprockets

RoR asset pipeline best practices for one-off JS plugins

I have a best practices question regarding one-off javascript plugins and their role in the Rails asset pipeline.
I'm new to Rails, and am working on a new project. The site template I'm using uses a large collection of js plugins for added functionality. (eq chartjs.org, ckeditor, and about 40 others) Since these are needed on a page-by-page basis, I'm not sure if I should really load them all in the application.js manifest.
Currently, I have the template's assets under the /vendor directory and only the core assets are being loaded from my application.js manifest.
Thoughts on how/where to include the plugins?
As i know that rails default added all js file include in application.js by //= require_tree . so, you can remove it and add only those file which you want to added. and if you want to run only specific function then you can use location.pathname with condition and it will work for only that specific page.

Rails 3.1 and Coffeescript: Require entire directory

There is a require for individual files like so:
//= require controllers/documents
Is there an equivalent for requiring a directory?
Yep:
//= require_tree controllers
By the way, you tagged this as coffeescript, but your question uses JavaScript comment syntax... so for the record, in CoffeeScript you want to use #=, not //=.
Also, this is actually a Sprockets question, not a Rails question. Sprockets is a default in Rails 3.1, but you can use it independently as well. At any rate, the Sprockets page offers documentation aplenty on require, require_tree, and many other handy directives.

Resources