teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

jqueryを追加

2020/10/22 16:09

投稿

Zengo_Master
Zengo_Master

スコア19

title CHANGED
File without changes
body CHANGED
@@ -396,6 +396,7 @@
396
396
  "@rails/activestorage": "^6.0.0-alpha",
397
397
  "@rails/ujs": "^6.0.0-alpha",
398
398
  "@rails/webpacker": "4.3.0",
399
+ "jquery": "^3.5.1",
399
400
  "turbolinks": "^5.2.0"
400
401
  },
401
402
  "version": "0.1.0",

4

package.jsonのコード追加

2020/10/22 16:09

投稿

Zengo_Master
Zengo_Master

スコア19

title CHANGED
File without changes
body CHANGED
@@ -386,6 +386,25 @@
386
386
  });
387
387
  ```
388
388
 
389
+ package.json
390
+ ```
391
+ {
392
+ "name": "kifu_app",
393
+ "private": true,
394
+ "dependencies": {
395
+ "@rails/actioncable": "^6.0.0-alpha",
396
+ "@rails/activestorage": "^6.0.0-alpha",
397
+ "@rails/ujs": "^6.0.0-alpha",
398
+ "@rails/webpacker": "4.3.0",
399
+ "turbolinks": "^5.2.0"
400
+ },
401
+ "version": "0.1.0",
402
+ "devDependencies": {
403
+ "webpack-dev-server": "^3.11.0"
404
+ }
405
+ }
406
+ ```
407
+
389
408
  ## ブラウザでの表示
390
409
  ![イメージ説明](315544b58ade49fe219241f99b41bfc5.png)
391
410
 

3

コードの追加

2020/10/22 13:00

投稿

Zengo_Master
Zengo_Master

スコア19

title CHANGED
File without changes
body CHANGED
@@ -80,6 +80,7 @@
80
80
  end
81
81
  ```
82
82
 
83
+ app/controllers/kifus_controller.rb
83
84
  ```ruby
84
85
  class KifusController < ApplicationController
85
86
  before_action :set_kifu, only: [:edit, :show, :destroy, :update]
@@ -182,6 +183,29 @@
182
183
  </div>
183
184
  ```
184
185
 
186
+ app/assets/stylesheets/index.scss
187
+ ```scss
188
+ .index-wrapper {
189
+ margin: 20px;
190
+ }
191
+
192
+ .new-link {
193
+ font-size: 20px;
194
+ }
195
+
196
+ h2 {
197
+ margin-top: 10px;
198
+ }
199
+
200
+ .kifu-lists {
201
+ margin-top: 5px;
202
+ }
203
+
204
+ .one-kifu {
205
+ font-size: 18px;
206
+ }
207
+ ```
208
+
185
209
  app/views/kifus/new.html.erb
186
210
  ```html
187
211
  <div class="new-wrapper">

2

全ファイルのコードを掲載

2020/10/20 06:26

投稿

Zengo_Master
Zengo_Master

スコア19

title CHANGED
File without changes
body CHANGED
@@ -72,35 +72,193 @@
72
72
  gem 'jquery-rails'
73
73
  ```
74
74
 
75
+ config/routes.rb
76
+ ```ruby
77
+ Rails.application.routes.draw do
75
- app/javascript/packs/application.js
78
+ root to: 'kifus#index'
79
+ resources :kifus
80
+ end
76
- ```js
81
+ ```
77
- // This file is automatically compiled by Webpack, along with any other files
78
- // present in this directory. You're encouraged to place your actual application logic in
79
- // a relevant structure within app/javascript and only use these pack files to reference
80
- // that code so it'll be compiled.
81
82
 
82
- require("@rails/ujs").start()
83
+ ```ruby
83
- // require("turbolinks").start()
84
+ class KifusController < ApplicationController
84
- require("@rails/activestorage").start()
85
+ before_action :set_kifu, only: [:edit, :show, :destroy, :update]
85
- require("channels")
86
86
 
87
- // Uncomment to copy all static images under ../images to the output folder and reference
88
- // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
89
- // or the `imagePath` JavaScript helper below.
90
- //
87
+ def index
91
- // const images = require.context('../images', true)
88
+ @kifus = Kifu.all.order('date DESC')
92
- // const imagePath = (name) => images(name, true)
93
- //= require zeroclipboard
94
- //= require jquery
95
- //= require rails-ujs
96
- //= require turbolinks
89
+ end
97
- //= require_tree .
98
90
 
91
+ def new
92
+ @kifu = Kifu.new
93
+ end
94
+
95
+ def create
99
- $(document).ready(function() {
96
+ @kifu = Kifu.create(kifu_params)
97
+ if @kifu.valid?
98
+ @kifu.save
99
+ redirect_to root_path
100
+ else
101
+ render 'new'
102
+ end
103
+ end
104
+
105
+ def edit
106
+ end
107
+
108
+ def update
109
+ if @kifu.update(kifu_params)
110
+ redirect_to kifu_path(@kifu.id)
111
+ else
112
+ render 'edit'
113
+ end
114
+ end
115
+
116
+ def show
117
+ end
118
+
119
+ def destroy
120
+ if @kifu.destroy
121
+ redirect_to root_path
122
+ else
123
+ redirect_to kifu_path(@kifu.id)
124
+ end
125
+ end
126
+
127
+ private
128
+
129
+ def kifu_params
100
- var clip = new ZeroClipboard($("#d_clip_button"))
130
+ params.require(:kifu).permit(:date, :opponent, :result_id, :type_id, :kifu)
101
- });
131
+ end
132
+
133
+ def set_kifu
134
+ @kifu = Kifu.find(params[:id])
135
+ end
136
+
137
+ end
102
138
  ```
103
139
 
140
+ app/views/kifus/index.html.erb
141
+ ```html
142
+ <div class="index-wrapper">
143
+
144
+ <div class="new-link">
145
+ <%= link_to '棋譜投稿ページへ', new_kifu_path %>
146
+ </div>
147
+
148
+ <h2><棋譜一覧></h2>
149
+
150
+ <div class="kifu-lists">
151
+ <% @kifus.each do |kifu| %>
152
+ <div class="one-kifu">
153
+
154
+ <%# 日付 %>
155
+ <span class="date">
156
+ <%= kifu.date %>
157
+ </span>
158
+
159
+ <%# 相手 %>
160
+ <span class="opponent">
161
+ <%= kifu.opponent %>
162
+ </span>
163
+
164
+ <%# 結果 %>
165
+ <span class="result">
166
+ <%= kifu.result.name %>
167
+ </span>
168
+
169
+ <%# 戦型 %>
170
+ <span class="type">
171
+ <%= kifu.type.name %>
172
+ </span>
173
+
174
+ <%# 棋譜リンク %>
175
+ <span class="kifu-page">
176
+ <%= link_to '棋譜', kifu_path(kifu.id) %>
177
+ </span>
178
+
179
+ </div>
180
+ <% end %>
181
+ </div>
182
+ </div>
183
+ ```
184
+
185
+ app/views/kifus/new.html.erb
186
+ ```html
187
+ <div class="new-wrapper">
188
+ <div class="input-forms">
189
+
190
+ <h2><対局情報入力></h2>
191
+
192
+ <%= form_with(model: @kifu, local: true) do |f| %>
193
+
194
+ <%# 日付 %>
195
+ <div class="input-field">
196
+ <%= f.label :date, '日付:' %>
197
+ <%= raw sprintf(
198
+ f.date_select(
199
+ :date,
200
+ use_month_numbers: true,
201
+ start_year: 2015,
202
+ end_year: 2025,
203
+ default: Date.today,
204
+ date_separator: '%s'),
205
+ '年 ', '月 ') + '日'
206
+ %>
207
+ </div>
208
+
209
+ <div class="input-field">
210
+ <%= f.label :opponent, '相手:' %>
211
+ <%= f.text_field :opponent %>
212
+ </div>
213
+
214
+ <div class="input-field">
215
+ <%= f.label :result_id, '結果:' %>
216
+ <%= f.collection_select :result_id, Result.all, :id, :name, {} %>
217
+ </div>
218
+
219
+ <div class="input-field">
220
+ <%= f.label :type_id, '戦型:' %>
221
+ <%= f.collection_select :type_id, Type.all, :id, :name, {} %>
222
+ </div>
223
+
224
+ <div class="input-field">
225
+ <%= f.label :kifu, '棋譜' %><br>
226
+ <%= f.text_area :kifu, placeholder: '棋譜を貼り付け', size: "35x5" %>
227
+ </div>
228
+
229
+ <div class="submit">
230
+ <%= f.submit "保存", class: 'submit-btn' %>
231
+ </div>
232
+
233
+ <div class="back">
234
+ <%= link_to "棋譜一覧へ戻る", root_path, class: "back-btn" %>
235
+ </div>
236
+ <% end %>
237
+ </div>
238
+ </div>
239
+ ```
240
+
241
+ app/assets/stylesheets/new.scss
242
+ ```scss
243
+ .new-wrapper {
244
+ margin: 20px;
245
+ }
246
+
247
+ .input-field {
248
+ margin-top: 20px;
249
+ }
250
+
251
+ .back {
252
+ font-size: 20px;
253
+ margin-top: 20px;
254
+ }
255
+
256
+ .submit-btn {
257
+ width: 50px;
258
+ height: 30px;
259
+ }
260
+ ```
261
+
104
262
  app/views/kifus/show.html.erb
105
263
  ```html
106
264
  <div class="show-wrapper">
@@ -135,6 +293,75 @@
135
293
  </div class="div">
136
294
  ```
137
295
 
296
+ app/assets/stylesheets/show.scss
297
+ ```scss
298
+ .show-wrapper {
299
+ margin: 20px;
300
+ }
301
+
302
+
303
+ .btns {
304
+ display: flex;
305
+ align-items: center;
306
+ margin-top: 20px;
307
+ }
308
+
309
+ .edit, .destroy, .analysis, .copy {
310
+ margin-right: 20px;
311
+ }
312
+
313
+ .edit-btn, .destroy-btn, .analysis-btn {
314
+ font-size: 20px;
315
+ font-weight: bold;
316
+ text-decoration: none;
317
+ background-color: yellow;
318
+ padding: 5px;
319
+ border: 2px solid black;
320
+ border-radius: 5px;
321
+ }
322
+
323
+ .edit-btn {
324
+ color: black;
325
+ }
326
+
327
+ .destroy-btn {
328
+ color: red;
329
+ }
330
+
331
+ .analysis-btn {
332
+ color: blue;
333
+ }
334
+ ```
335
+
336
+ app/javascript/packs/application.js
337
+ ```js
338
+ // This file is automatically compiled by Webpack, along with any other files
339
+ // present in this directory. You're encouraged to place your actual application logic in
340
+ // a relevant structure within app/javascript and only use these pack files to reference
341
+ // that code so it'll be compiled.
342
+
343
+ require("@rails/ujs").start()
344
+ // require("turbolinks").start()
345
+ require("@rails/activestorage").start()
346
+ require("channels")
347
+
348
+ // Uncomment to copy all static images under ../images to the output folder and reference
349
+ // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
350
+ // or the `imagePath` JavaScript helper below.
351
+ //
352
+ // const images = require.context('../images', true)
353
+ // const imagePath = (name) => images(name, true)
354
+ //= require zeroclipboard
355
+ //= require jquery
356
+ //= require rails-ujs
357
+ //= require turbolinks
358
+ //= require_tree .
359
+
360
+ $(document).ready(function() {
361
+ var clip = new ZeroClipboard($("#d_clip_button"))
362
+ });
363
+ ```
364
+
138
365
  ## ブラウザでの表示
139
366
  ![イメージ説明](315544b58ade49fe219241f99b41bfc5.png)
140
367
 

1

jQuery導入後のコード

2020/10/20 06:25

投稿

Zengo_Master
Zengo_Master

スコア19

title CHANGED
File without changes
body CHANGED
@@ -8,6 +8,70 @@
8
8
  # 検証作業と結果
9
9
  上記のQiitaに沿って進めました。以下は現在のコードです。
10
10
 
11
+ Gemfile
12
+ ```
13
+ source 'https://rubygems.org'
14
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
15
+
16
+ ruby '2.6.5'
17
+
18
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
19
+ gem 'rails', '~> 6.0.0'
20
+ # Use mysql as the database for Active Record
21
+ # gem 'mysql2', '>= 0.4.4'
22
+ gem 'mysql2', '0.5.3'
23
+ # Use Puma as the app server
24
+ gem 'puma', '~> 3.11'
25
+ # Use SCSS for stylesheets
26
+ gem 'sass-rails', '~> 5'
27
+ # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
28
+ gem 'webpacker', '~> 4.0'
29
+ # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
30
+ gem 'turbolinks', '~> 5'
31
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
32
+ gem 'jbuilder', '~> 2.7'
33
+ # Use Redis adapter to run Action Cable in production
34
+ # gem 'redis', '~> 4.0'
35
+ # Use Active Model has_secure_password
36
+ # gem 'bcrypt', '~> 3.1.7'
37
+
38
+ # Use Active Storage variant
39
+ # gem 'image_processing', '~> 1.2'
40
+
41
+ # Reduces boot times through caching; required in config/boot.rb
42
+ gem 'bootsnap', '>= 1.4.2', require: false
43
+
44
+ group :development, :test do
45
+ # Call 'byebug' anywhere in the code to stop execution and get a debugger console
46
+ gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
47
+ end
48
+
49
+ group :development do
50
+ # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
51
+ gem 'web-console', '>= 3.3.0'
52
+ gem 'listen', '>= 3.0.5', '< 3.2'
53
+ # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
54
+ gem 'spring'
55
+ gem 'spring-watcher-listen', '~> 2.0.0'
56
+ gem 'rubocop', require: false
57
+ end
58
+
59
+ group :test do
60
+ # Adds support for Capybara system testing and selenium driver
61
+ gem 'capybara', '>= 2.15'
62
+ gem 'selenium-webdriver'
63
+ # Easy installation and use of web drivers to run system tests with browsers
64
+ gem 'webdrivers'
65
+ end
66
+
67
+ # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
68
+ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
69
+
70
+ gem 'active_hash'
71
+ gem 'zeroclipboard-rails'
72
+ gem 'jquery-rails'
73
+ ```
74
+
11
75
  app/javascript/packs/application.js
12
76
  ```js
13
77
  // This file is automatically compiled by Webpack, along with any other files
@@ -27,6 +91,10 @@
27
91
  // const images = require.context('../images', true)
28
92
  // const imagePath = (name) => images(name, true)
29
93
  //= require zeroclipboard
94
+ //= require jquery
95
+ //= require rails-ujs
96
+ //= require turbolinks
97
+ //= require_tree .
30
98
 
31
99
  $(document).ready(function() {
32
100
  var clip = new ZeroClipboard($("#d_clip_button"))