Search code examples
javascriptruby-on-railszeroclipboard

Zeroclipboard-rails not working but no error


I'm trying to use Zeroclipboard (via gem zeroclipboard-rails) in my project, but it doesn't work and I have no error.

This is my script:

$(document).on("ready page:change", function () {
  var client = new ZeroClipboard($("#copyToClipboard"));

  client.on("complete", function (client, args) {
    alert("Text successfully copied");
  });
});

$(document).on("page:before-change", function () {
  ZeroClipboard.destroy();
});

I also tried:

$(document).on("ready page:change", function () {
  var client = new ZeroClipboard($("#copyToClipboard"));

  client.on("load", function (client) {
    client.on("complete", function (client, args) {
      alert("Text successfully copied");
    });
  });
});

$(document).on("page:before-change", function () {
  ZeroClipboard.destroy();
});

I'm able to log the client var but nothing within complete event.

I found this doc: https://github.com/zeroclipboard/zeroclipboard/blob/master/docs/api/ZeroClipboard.md

So i tried to replace load with ready, then complete with aftercopy but no effects...

My slim code:

#contentForClipboard.d-none
  = simple_format(h proposition.content)

[...]

= button_tag id: 'copyToClipboard', 'data-clipboard-target' => 'contentForClipboard', class: "btn btn-light btn-sm clip_button" do
  => fa_icon('fas', 'clipboard')
  | Copy

Solution

  • zeroclipboard relies on Adobe Flash, that is being phased out (EOL scheduled in 2020), and already it is not enabled by default in modern browsers.

    You can try libraries like clipboard.js that rely on modern browser support (Selection and execCommand) and do not require any flash.