2

When I first time load my page, select2 will work properly... But if I go to other links, we wouldn't be able to see select2 unless we reload the page.

Here's my code:

Gemfile:

gem 'turbolinks', '~> 5.0.0'
gem "select2-rails"

application.js:

//= require turbolinks
//= require select2

application.css:

*= require select2

index.html.erb:

<script>
  $(document).ready(function() { $("#e1").select2(); });
</script>

1 Answers1

1

Since you're using turbolinks, you need to change the structure on ready to

<script>
  $(document).on("turbolinks:load", function() { 
    $("#e1").select2(); 
  });
</script>

In your application.js, prefer to require select2 before turbolinks.

//= require select2
//= require turbolinks
Yewness
  • 322
  • 1
  • 4
  • 14