Is `require jquery_ujs` still needed in Rails 5.1?

4.7k views Asked by At

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?

2

There are 2 answers

2
Kit On

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

0
agbodike On

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!