argerim / select2-rails

Integrate Select2 javascript library with Rails asset pipeline
https://github.com/argerim/select2-rails
965 stars 374 forks source link

failed in loading js after back button #169

Closed sondh5 closed 5 years ago

sondh5 commented 7 years ago

I am using select2 with ransack to multiple searching but it got a big problem.

My js file: search.js $(document).on("turbolinks:load", function () { $("#q_city_matches_any").select2({ placeholder: I18n.t("js.city") }); });

My slim: index.html.slim

p.form-group= f.select :city_matches_any, @cities, {}, {class: "form-control", multiple: true}

application.js

Its oke in normal case but problem happens when I move from index page to other page and click button back of browser to back to index page.

Normal case: https://gyazo.com/9ea59e58e5271cd27ff716e548f51f17 Issue: https://gyazo.com/3d8826be1b4f028760618e83e8d7b9cf

Thank you for your support!

argerim commented 7 years ago

@hongsondhs can try with https://github.com/kossnocorp/jquery.turbolinks ? pls

rjducker commented 6 years ago

Hello! I Have same problem on rails 5. jquery.turbolinks is deprecated, this gem does not work with Turbolinks 5+

rjducker commented 6 years ago

This works for me:

$(document).on("turbolinks:before-cache", function() {
    $('.select2-input').select2('destroy');
});

(document).on('turbolinks:load',function(){
  $('.select2-input').select2();
});

Details here