1

I am trying to use the rails select2 gem, gem 'select2-rails' But when I try to use it with the following order import order, my browser complains that select2 is not a function. From what I have found this order should be correct.

javascripts/application.js

//= require rails-ujs
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require select2-full
//= require_tree .

$(document).on('turbolinks:load', function() {
   console.log('(document).turbolinks:load')
   $("#estimation_patient_id").select2({
       theme: "bootstrap"
   });
});
Pierre
  • 1,114
  • 2
  • 10
  • 24

1 Answers1

1

You can use

//= require rails-ujs

OR

//= require jquery_ujs

not needed to both

And modify like below

(function($){
    $(document).on('turbolinks:load', function() {
       $("#estimation_patient_id").select2({
           theme: "bootstrap"
       });
    });
}(jQuery));

Remember it: sometimes //= require select2-full is not working but //= require select2 is working nicely with all requirements. I recommend to use //= require select2.

fool-dev
  • 7,671
  • 9
  • 40
  • 54
  • I have the exact same order of my require statements as my question, but it works with the function in your answer. thx – Pierre Mar 01 '18 at 10:32