I am installing jQuery in my 5.1.x
Rails app via the jquery-rails
gem.
In the gem setup, they recommend to add these lines to application.js
by default:
//= require jquery
//= require jquery_ujs
But, in a Rails 5.1.x
app, you have already this line which doesn't depend on jQuery anymore:
//= require rails-ujs
I suppose both are doing the exact same thing and one is not needed.
Should I keep both anyway or should I prefer only jquery_ujs
or only rails-ujs
?
you only need require rails_ujs , otherwise if you are doing any type of submission from client (for example), an alert will process twice.
This is a simple app that displays a list of tutorials saved in the database. Starting Rails 5.1, jQuery is not included by default.
The UJS in jquery-ujs stands for unobtrusive JavaScript. This is a rather broad term that generally refers to using JavaScript to progressively enhance the user experience for capable browsers without negatively impacting clients that do not support or do not enable JavaScript.
jquery-ujs
is a thing of the past as of Rails 5.1, you don't need it.
As of Rails 5.1 jQuery is no longer required for UJS (unobtrusive javascript). So if you have no need of jQuery in your rails app, you can just use
//= require rails-ujs
On the other hand, if you do use jQuery in your app, and use the jquery-rails
gem, and you should NOT require rails-ujs
, but should instead use:
//= require jquery
//= require jquery_ujs
Requiring jquery_ujs
along with jQuery can cause issues in the app, and you may see the following JS console error:
Uncaught Error: jquery-ujs has already been loaded!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With