argerim/select2-rails

failed in loading js after back button

Closed this issue · 3 comments

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

  • //= require jquery
  • //= require jquery_ujs
  • //= require bootstrap-sprockets
  • //= require turbolinks
  • //= require select2
  • //= require i18n
  • //= require i18n.js
  • //= require i18n/translations
  • //= require_tree .

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!

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

This works for me:

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

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

Details here