47
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails]「clipboardにコピーする」ボタンを超簡単に設置

Last updated at Posted at 2014-01-23

概要

Githubでおなじみのアレですね。

スクリーンショット_2014_01_23_14_11.png

これ。

zeroclipboard-railsというgemを使えば一発です。

zeroclipboard-rails
https://github.com/zeroclipboard/zeroclipboard-rails

導入手順

Gemfileに追記して、

Gemfile
gem 'zeroclipboard-rails'

インストール。

$ bundle

application.jsに以下追記。

assets/javascripts/application.js
//= require zeroclipboard

$(document).ready(function() {
  var clip = new ZeroClipboard($("#d_clip_button"))
});

あとはコピー領域(textarea)と、コピーボタン(button)を用意するだけ。

erb
<div>
  <textarea id="fe_text" name="" rows="7" cols="100">コピー領域</textarea>
  <button class='my_clip_button' data-clipboard-target='fe_text' data-clipboard-text='Default clipboard text from attribute' id='d_clip_button'>
    <b>コピーする</b>
  </button>
</div>

超簡単!

47
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?