All threads / Rails, turbolinks and javascript. How to avoid duplicate Switchery button appear?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rails, turbolinks and javascript. How to avoid duplicate Switchery button appear?

Lee Terng Gio asked in Javascript

I'm implementing Switchery button in my Rails app and Turbolinks is on. When I navigate to another page, and then click the back button, the button is being duplicated.

Here is my Javascript code:

$(document).on('turbolinks:load', function() {
var elem = document.querySelector('.js-switch');
var switchery = new Switchery(elem, {className:"switchery switchery-small"});
});

Html view:
<b>Assign as administrator?<span> <%= f.check_box :admin, class:"js-switch" %></span></b>

Screenshot: ![https://i.stack.imgur.com/ePJdk.png]

So how can i handle this problem ?

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.