解決したいこと
Railsでtextareaの内容をコピーするボタンを設置したいです。
調べた内容
[Rails]「clipboardにコピーする」ボタンを超簡単に設置
https://qiita.com/kidach1/items/0cef215de8ff47221f24
検証作業と結果
上記のQiitaに沿って進めました。以下は現在のコードです。
Gemfile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.0' # Use mysql as the database for Active Record # gem 'mysql2', '>= 0.4.4' gem 'mysql2', '0.5.3' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 4.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' gem 'rubocop', require: false end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'active_hash' gem 'zeroclipboard-rails' gem 'jquery-rails'
config/routes.rb
ruby
1Rails.application.routes.draw do 2 root to: 'kifus#index' 3 resources :kifus 4end
app/controllers/kifus_controller.rb
ruby
1class KifusController < ApplicationController 2 before_action :set_kifu, only: [:edit, :show, :destroy, :update] 3 4 def index 5 @kifus = Kifu.all.order('date DESC') 6 end 7 8 def new 9 @kifu = Kifu.new 10 end 11 12 def create 13 @kifu = Kifu.create(kifu_params) 14 if @kifu.valid? 15 @kifu.save 16 redirect_to root_path 17 else 18 render 'new' 19 end 20 end 21 22 def edit 23 end 24 25 def update 26 if @kifu.update(kifu_params) 27 redirect_to kifu_path(@kifu.id) 28 else 29 render 'edit' 30 end 31 end 32 33 def show 34 end 35 36 def destroy 37 if @kifu.destroy 38 redirect_to root_path 39 else 40 redirect_to kifu_path(@kifu.id) 41 end 42 end 43 44 private 45 46 def kifu_params 47 params.require(:kifu).permit(:date, :opponent, :result_id, :type_id, :kifu) 48 end 49 50 def set_kifu 51 @kifu = Kifu.find(params[:id]) 52 end 53 54end
app/views/kifus/index.html.erb
html
1<div class="index-wrapper"> 2 3 <div class="new-link"> 4 <%= link_to '棋譜投稿ページへ', new_kifu_path %> 5 </div> 6 7 <h2><棋譜一覧></h2> 8 9 <div class="kifu-lists"> 10 <% @kifus.each do |kifu| %> 11 <div class="one-kifu"> 12 13 <%# 日付 %> 14 <span class="date"> 15 <%= kifu.date %> 16 </span> 17 18 <%# 相手 %> 19 <span class="opponent"> 20 <%= kifu.opponent %> 21 </span> 22 23 <%# 結果 %> 24 <span class="result"> 25 <%= kifu.result.name %> 26 </span> 27 28 <%# 戦型 %> 29 <span class="type"> 30 <%= kifu.type.name %> 31 </span> 32 33 <%# 棋譜リンク %> 34 <span class="kifu-page"> 35 <%= link_to '棋譜', kifu_path(kifu.id) %> 36 </span> 37 38 </div> 39 <% end %> 40 </div> 41</div>
app/assets/stylesheets/index.scss
scss
1.index-wrapper { 2 margin: 20px; 3} 4 5.new-link { 6 font-size: 20px; 7} 8 9h2 { 10 margin-top: 10px; 11} 12 13.kifu-lists { 14 margin-top: 5px; 15} 16 17.one-kifu { 18 font-size: 18px; 19}
app/views/kifus/new.html.erb
html
1<div class="new-wrapper"> 2 <div class="input-forms"> 3 4 <h2><対局情報入力></h2> 5 6 <%= form_with(model: @kifu, local: true) do |f| %> 7 8 <%# 日付 %> 9 <div class="input-field"> 10 <%= f.label :date, '日付:' %> 11 <%= raw sprintf( 12 f.date_select( 13 :date, 14 use_month_numbers: true, 15 start_year: 2015, 16 end_year: 2025, 17 default: Date.today, 18 date_separator: '%s'), 19 '年 ', '月 ') + '日' 20 %> 21 </div> 22 23 <div class="input-field"> 24 <%= f.label :opponent, '相手:' %> 25 <%= f.text_field :opponent %> 26 </div> 27 28 <div class="input-field"> 29 <%= f.label :result_id, '結果:' %> 30 <%= f.collection_select :result_id, Result.all, :id, :name, {} %> 31 </div> 32 33 <div class="input-field"> 34 <%= f.label :type_id, '戦型:' %> 35 <%= f.collection_select :type_id, Type.all, :id, :name, {} %> 36 </div> 37 38 <div class="input-field"> 39 <%= f.label :kifu, '棋譜' %><br> 40 <%= f.text_area :kifu, placeholder: '棋譜を貼り付け', size: "35x5" %> 41 </div> 42 43 <div class="submit"> 44 <%= f.submit "保存", class: 'submit-btn' %> 45 </div> 46 47 <div class="back"> 48 <%= link_to "棋譜一覧へ戻る", root_path, class: "back-btn" %> 49 </div> 50 <% end %> 51 </div> 52</div>
app/assets/stylesheets/new.scss
scss
1.new-wrapper { 2 margin: 20px; 3} 4 5.input-field { 6 margin-top: 20px; 7} 8 9.back { 10 font-size: 20px; 11 margin-top: 20px; 12} 13 14.submit-btn { 15 width: 50px; 16 height: 30px; 17}
app/views/kifus/show.html.erb
html
1<div class="show-wrapper"> 2 3 <div class="kifu-text"> 4 <textarea id="fe_text" name="" rows="10" cols="40"><%= @kifu.kifu %></textarea> 5 <button class='my_clip_button' data-clipboard-target='fe_text' data-clipboard-text='Default clipboard text from attribute' id='d_clip_button'> 6 <b>コピー</b> 7 </button> 8 </div> 9 10 <strong>※解析は棋譜をコピーしてから押してね</strong> 11 12 <div class="btns"> 13 <div class="edit"> 14 <%= link_to "編集", edit_kifu_path(@kifu.id), method: :get, class: "edit-btn" %> 15 </div> 16 17 <div class="destroy"> 18 <%= link_to "削除", kifu_path(@kifu.id), method: :delete, class: "destroy-btn" %> 19 </div> 20 21 <div class="analysis"> 22 <%= link_to '解析', 'https://www.shogi-extend.com/adapter', class: "analysis-btn" %> 23 </div> 24 </div> 25 26 <div class="back"> 27 <%= link_to '棋譜一覧へ戻る', root_path, class: "back-btn" %> 28 </div> 29 30</div class="div">
app/assets/stylesheets/show.scss
scss
1.show-wrapper { 2 margin: 20px; 3} 4 5 6.btns { 7 display: flex; 8 align-items: center; 9 margin-top: 20px; 10} 11 12.edit, .destroy, .analysis, .copy { 13 margin-right: 20px; 14} 15 16.edit-btn, .destroy-btn, .analysis-btn { 17 font-size: 20px; 18 font-weight: bold; 19 text-decoration: none; 20 background-color: yellow; 21 padding: 5px; 22 border: 2px solid black; 23 border-radius: 5px; 24} 25 26.edit-btn { 27 color: black; 28} 29 30.destroy-btn { 31 color: red; 32} 33 34.analysis-btn { 35 color: blue; 36}
app/javascript/packs/application.js
js
1// This file is automatically compiled by Webpack, along with any other files 2// present in this directory. You're encouraged to place your actual application logic in 3// a relevant structure within app/javascript and only use these pack files to reference 4// that code so it'll be compiled. 5 6require("@rails/ujs").start() 7// require("turbolinks").start() 8require("@rails/activestorage").start() 9require("channels") 10 11// Uncomment to copy all static images under ../images to the output folder and reference 12// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) 13// or the `imagePath` JavaScript helper below. 14// 15// const images = require.context('../images', true) 16// const imagePath = (name) => images(name, true) 17//= require zeroclipboard 18//= require jquery 19//= require rails-ujs 20//= require turbolinks 21//= require_tree . 22 23$(document).ready(function() { 24 var clip = new ZeroClipboard($("#d_clip_button")) 25});
package.json
{ "name": "kifu_app", "private": true, "dependencies": { "@rails/actioncable": "^6.0.0-alpha", "@rails/activestorage": "^6.0.0-alpha", "@rails/ujs": "^6.0.0-alpha", "@rails/webpacker": "4.3.0", "jquery": "^3.5.1", "turbolinks": "^5.2.0" }, "version": "0.1.0", "devDependencies": { "webpack-dev-server": "^3.11.0" } }
ブラウザでの表示
コピーできない
コピーボタンを押しても、コピーされていません。原因と対処法を教えていただきたいです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/19 17:00
2020/10/19 23:53
2020/10/20 00:52
2020/10/20 03:21
2020/10/20 03:40
2020/10/20 03:51
2020/10/20 03:51
2020/10/20 06:28
2020/10/22 12:58 編集
2020/10/22 13:04
2020/10/22 13:13
2020/10/22 13:18
2020/10/22 16:27