Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `require jquery_ujs` still needed in Rails 5.1?

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?

like image 587
Hartator Avatar asked Jun 09 '17 20:06

Hartator


People also ask

Do I need 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.

Does Rails come with jQuery?

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.

What is Jquery_ujs?

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.


2 Answers

jquery-ujs is a thing of the past as of Rails 5.1, you don't need it.

like image 191
Kit Avatar answered Oct 22 '22 06:10

Kit


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!

like image 29
agbodike Avatar answered Oct 22 '22 08:10

agbodike