質問編集履歴

2

タイトルの修正

2019/04/16 09:50

投稿

kazuki0714
kazuki0714

スコア28

test CHANGED
@@ -1 +1 @@
1
- 【Rails】情報を追加する際、2つ目を登録すると1つ目の内容に上書きされないようにするためには?
1
+ 【Railsのループ処理】情報を追加する際、2つ目を登録すると1つ目の内容に上書きされないようにするためには?
test CHANGED
@@ -30,6 +30,12 @@
30
30
 
31
31
 
32
32
 
33
+ 「スワッチを追加」の下のループ処理 (@posts.each do |post|)がそもそも間違っているのでしょうか?
34
+
35
+
36
+
37
+
38
+
33
39
  ![イメージ説明](f65b0f717af7e4fde3abc98bdf5404f7.png)
34
40
 
35
41
 

1

タイトルの変更、分かりづらいため、最初のコード表を削除

2019/04/16 09:50

投稿

kazuki0714
kazuki0714

スコア28

test CHANGED
@@ -1 +1 @@
1
- 【Rails】1ユーザーにき、スワッチ(見本)データ複数登録する場合コードの書き
1
+ 【Rails】情報を追加する際、2を登録すると1つ目内容に上書きされないようにするためには?
test CHANGED
@@ -14,319 +14,393 @@
14
14
 
15
15
 
16
16
 
17
+
18
+
19
+ 【ここで壁にぶつかってます】
20
+
17
- 以下が顧客一覧画面です。詳細ボタンすと詳細ページ(customers/:id)へ飛びます。
21
+ 顧客詳細ページの「スワッチ追加」からスワッチページに飛んで登録このページになります。
22
+
18
-
23
+ 1つしか登録していないのに同じ内容が4つ表示されています。「スワッチを追加」の下の4つの表です。
24
+
25
+
26
+
19
-
27
+ また2つめを登録すると1つめが上書きされて、2つめの情報が4つ表示されます。
28
+
20
-
29
+ 顧客一覧で4人登録されているので、そうなっているのだと思いますが解決方法が分からず。。。
30
+
31
+
32
+
21
- ![](209cb1cc8d97ddedc6eb637a9d26b278.png)
33
+ ![イメージ説明](f65b0f717af7e4fde3abc98bdf5404f7.png)
34
+
35
+
22
36
 
23
37
 
24
38
 
25
39
  ```Ruby
26
40
 
27
- # views/posts/index.html.erb
41
+ # app/views/posts/show.html.erb
28
-
42
+
43
+
44
+
29
- <h1>顧客一覧</h1>
45
+ <h1> 顧客詳細</h1>
30
-
46
+
47
+
48
+
31
- <div class="main posts-index">
49
+ <div class="main posts-show">
50
+
32
-
51
+ <div class="container"> # 顧客情報を登録したら表示される
52
+
53
+ <div class="posts-show-item">
54
+
55
+
56
+
57
+ <p>会社名:<%= @post.company %></p><br>
58
+
59
+ <p>業態:<%= @post.category %></p><br>
60
+
61
+ <p>お客様名:<%= @post.name %></p><br>
62
+
63
+ <p>担当者:<%= @post.chargeName %></p><br>
64
+
65
+ <p>備考:<%= @post.other %></p><br>
66
+
67
+ <p>名刺:<%= @post.image_name %></p><br>
68
+
69
+ <%= image_tag "/images/#{@post.id}.jpg", :size =>'320x240'%>
70
+
71
+
72
+
73
+ </div>
74
+
75
+
76
+
33
- <div class="container">
77
+ <div class="post-menus">
78
+
79
+ <%= link_to("編集", "/customers/#{@post.id}/edit") %>
80
+
81
+ <%= link_to("削除", "/customers/#{@post.id}/destroy",{method:"post"}) %>
82
+
83
+
84
+
85
+ </div>
86
+
87
+
88
+
89
+ <%= link_to("スワッチを追加", "/customers/#{@post.id}/orders/new") %>
90
+
91
+
92
+
93
+ <div class="container"> #スワッチを登録したら表示される
34
94
 
35
95
  <% @posts.each do |post| %>
36
96
 
97
+
98
+
37
- <div class="posts-index-item">
99
+ <div class="posts-swatch-item">
38
-
100
+
39
- <table border="1">
101
+ <table border="1">
40
-
102
+
41
- <tr>
103
+ <tr>
42
-
104
+
43
- <th>id</th>
105
+ <th>No</th>
44
-
106
+
45
- <th>会社名</th>
107
+ <th>Vol</th>
46
-
108
+
47
- <th>業態</th>
109
+ <th>品番</th>
48
-
49
- <th>お客様名</th>
110
+
50
-
51
- <th>担当者</th>
52
-
53
- <th>備考</th>
54
-
55
- <th></th>
111
+ <th></th>
56
-
112
+
57
- </tr>
113
+ </tr>
58
-
59
-
60
-
114
+
115
+
116
+
61
- <tr>
117
+ <tr>
62
-
118
+
63
- <td><%= post.id %></td>
119
+ <td>row-counter</td>
64
-
65
- <td><%= post.company %></td>
120
+
66
-
67
- <td><%= post.category %></td>
121
+ <td><%= @post.collection %></td>
68
-
122
+
69
- <td><%= post.name %></td>
123
+ <td><%= @post.number %></td>
70
-
71
- <td><%= post.chargeName %></td>
124
+
72
-
73
- <td><%= post.other %></td>
74
-
75
- <td><%= link_to("詳細", "/customers/#{post.id}") %></td>
125
+ <td><%= link_to("詳細", "/customers/#{@post.id}") %></td>
76
-
126
+
77
- </tr>
127
+ </tr>
78
-
128
+
79
- </table>
129
+ </table>
130
+
80
-
131
+ </div>
81
-
82
132
 
83
133
  <% end %>
84
134
 
135
+
136
+
137
+ </div>
138
+
139
+
140
+
141
+
142
+
85
143
  </div>
86
144
 
87
145
  </div>
88
146
 
89
147
  ```
90
148
 
91
- 【ここで壁にぶつかってます】
149
+
92
-
150
+
93
- 詳細ページの「スワッチを追加」からスワッチページに飛んで登録のページになります。
151
+ 上記画面から「スワッチを追加」を押すと以下画像ページに飛びます。
94
-
95
- 1つしか登録していないのに4つ登録されています。
152
+
96
-
97
- また2つめを登録す1つめが上書きされて、2つめの情報が4つ表示されます。
153
+ このページの登録ボタンを押すと再び詳細ページ(/customers/:id)に戻ります。
98
-
99
- 顧客一覧で4人登録されているので、そうなっているのだと思いますが解決方法が分からず。。。
154
+
100
-
101
-
102
-
155
+
156
+
103
- ![イメージ説明](f65b0f717af7e4fde3abc98bdf5404f7.png)
157
+ ![イメージ説明](90d0c143062ef689a2760d1fd6a1025d.png)
104
-
105
-
106
158
 
107
159
 
108
160
 
109
161
  ```Ruby
110
162
 
111
- # app/views/posts/show.html.erb
112
-
113
-
114
-
115
- <h1> 顧客詳細</h1>
116
-
117
-
118
-
119
- <div class="main posts-show">
120
-
121
- <div class="container"> # 顧客情報を登録したら表示される
122
-
123
- <div class="posts-show-item">
124
-
125
-
126
-
127
- <p>会社名:<%= @post.company %></p><br>
128
-
129
- <p>業態:<%= @post.category %></p><br>
130
-
131
- <p>お客様名:<%= @post.name %></p><br>
132
-
133
- <p>担当者:<%= @post.chargeName %></p><br>
134
-
135
- <p>備考:<%= @post.other %></p><br>
136
-
137
- <p>名刺:<%= @post.image_name %></p><br>
138
-
139
- <%= image_tag "/images/#{@post.id}.jpg", :size =>'320x240'%>
140
-
141
-
142
-
143
- </div>
144
-
145
-
146
-
147
- <div class="post-menus">
148
-
149
- <%= link_to("編集", "/customers/#{@post.id}/edit") %>
150
-
151
- <%= link_to("削除", "/customers/#{@post.id}/destroy",{method:"post"}) %>
152
-
153
-
154
-
155
- </div>
156
-
157
-
158
-
159
- <%= link_to("スワッチを追加", "/customers/#{@post.id}/orders/new") %>
160
-
161
-
162
-
163
- <div class="container"> #スワッチを登録したら表示される
164
-
165
- <% @posts.each do |post| %>
166
-
167
-
168
-
169
- <div class="posts-swatch-item">
170
-
171
- <table border="1">
172
-
173
- <tr>
174
-
175
- <th>No</th>
176
-
177
- <th>Vol</th>
178
-
179
- <th>品番</th>
180
-
181
- <th></th>
182
-
183
- </tr>
184
-
185
-
186
-
187
- <tr>
188
-
189
- <td>row-counter</td>
190
-
191
- <td><%= @post.collection %></td>
192
-
193
- <td><%= @post.number %></td>
194
-
195
- <td><%= link_to("詳細", "/customers/#{@post.id}") %></td>
196
-
197
- </tr>
198
-
199
- </table>
200
-
201
- </div>
202
-
203
- <% end %>
204
-
205
-
206
-
207
- </div>
208
-
209
-
210
-
211
-
212
-
213
- </div>
214
-
215
- </div>
163
+ # views/posts/swatch.html.erb
164
+
165
+ <%= form_tag("/customers/#{@post.id}/swatch_create",{multipart: true}) do %>
166
+
167
+
168
+
169
+ <h1>スワッチ登録</h1>
170
+
171
+ <p>以下の顧客のPICKUPスワッチを登録します。</p>
172
+
173
+
174
+
175
+ <p>会社名:<%= @post.company %></p><br>
176
+
177
+ <p>お客様名:<%= @post.name %></p><br>
178
+
179
+ <p>担当者:<%= @post.chargeName %></p><br>
180
+
181
+
182
+
183
+ <p>
184
+
185
+ <select name="collection" required>
186
+
187
+ <option value="vol.1">vol.1</option>
188
+
189
+ <option value="vol.2">vol.2</option>
190
+
191
+ <option value="vol.3">vol.3</option>
192
+
193
+ </select>
194
+
195
+ </p>
196
+
197
+
198
+
199
+
200
+
201
+ <p>登録するコレクションを選んでください</p>
202
+
203
+
204
+
205
+ <p>スワッチ1</p>
206
+
207
+
208
+
209
+ <input name="number" placeholder="品番を入力してください"><br>
210
+
211
+
212
+
213
+ <input name="swatch_note" placeholder="備考"><br>
214
+
215
+
216
+
217
+
218
+
219
+ <p><input type="submit" value="登録"></p>
220
+
221
+
222
+
223
+ <% end %>
224
+
225
+
216
226
 
217
227
  ```
218
228
 
219
-
220
-
221
- 上記の画面から「スワッチを追加」を押すと以下の画像ページ飛びます。
229
+ routes.rbposts_controller は以下になります。
222
-
223
- このページの登録ボタンを押すと再び詳細ページ(/customers/:id)に戻ります。
224
-
225
-
226
-
227
- ![イメージ説明](90d0c143062ef689a2760d1fd6a1025d.png)
228
230
 
229
231
 
230
232
 
231
233
  ```Ruby
232
234
 
235
+ #routes.rb
236
+
237
+ Rails.application.routes.draw do
238
+
233
- # views/posts/swatch.html.erb
239
+ get '/' => "home#top"
240
+
234
-
241
+ get '/customers' => "posts#index" #顧客一覧
242
+
243
+ get '/customers/new' => "posts#new" #顧客登録
244
+
245
+ post '/customers/create' => "posts#create" #登録ボタン押したときのアクション
246
+
247
+ get '/customers/:id/edit' => "posts#edit" #顧客情報編集
248
+
249
+ post '/customers/:id/update' => "posts#update" #更新
250
+
235
- <%= form_tag("/customers/#{@post.id}/swatch_create",{multipart: true}) do %>
251
+ post "/customers/:id/destroy" => "posts#destroy" #削除
236
-
237
-
238
-
239
- <h1>スワッチ登録</h1>
252
+
240
-
241
- <p>以下の顧客のPICKUPスワッチを登録します。</p>
253
+
242
-
243
-
244
-
245
- <p>会社名:<%= @post.company %></p><br>
254
+
246
-
247
- <p>お客様名:<%= @post.name %></p><br>
248
-
249
- <p>担当者:<%= @post.chargeName %></p><br>
250
-
251
-
252
-
253
- <p>
254
-
255
- <select name="collection" required>
256
-
257
- <option value="vol.1">vol.1</option>
258
-
259
- <option value="vol.2">vol.2</option>
260
-
261
- <option value="vol.3">vol.3</option>
262
-
263
- </select>
264
-
265
- </p>
266
-
267
-
268
-
269
-
270
-
271
- <p>登録するコレクションを選んでください</p>
272
-
273
-
274
-
275
- <p>スワッチ1</p>
276
-
277
-
278
-
279
- <input name="number" placeholder="品番を入力してください"><br>
280
-
281
-
282
-
283
- <input name="swatch_note" placeholder="備考"><br>
284
-
285
-
286
-
287
-
288
-
289
- <p><input type="submit" value="登録"></p>
255
+ get '/customers/:id' => "posts#show" #顧客詳細画面
290
-
291
-
292
-
256
+
293
- <% end %>
257
+ get '/customers/:id/orders/new' => "posts#swatch" #スワッチ登録画面
258
+
259
+ post '/customers/:id/swatch_create' => "posts#swatch_create" #登録ボタン押したときのアクション
294
260
 
295
261
 
296
262
 
297
263
  ```
298
264
 
299
- routes.rbとposts_controller は以下になります。
300
-
301
265
 
302
266
 
303
267
  ```Ruby
304
268
 
305
- #routes.rb
306
-
307
- Rails.application.routes.draw do
308
-
309
- get '/' => "home#top"
310
-
311
- get '/customers' => "posts#index" #顧客一覧
312
-
313
- get '/customers/new' => "posts#new" #顧客登録
314
-
315
- post '/customers/create' => "posts#create" #登録ボタン押したときのアクション
316
-
317
- get '/customers/:id/edit' => "posts#edit" #顧客情報編集
318
-
319
- post '/customers/:id/update' => "posts#update" #更新
320
-
321
- post "/customers/:id/destroy" => "posts#destroy" #削除
322
-
323
-
324
-
325
- get '/customers/:id' => "posts#show" #顧客詳細画面
326
-
327
- get '/customers/:id/orders/new' => "posts#swatch" #スワッチ登録画面
328
-
329
- post '/customers/:id/swatch_create' => "posts#swatch_create" #登録ボタン押したときのアクション
269
+ class PostsController < ApplicationController
270
+
271
+ def index
272
+
273
+ @posts = Post.all
274
+
275
+ end
276
+
277
+
278
+
279
+ def new
280
+
281
+ end
282
+
283
+
284
+
285
+ def create
286
+
287
+ @post = Post.new(company: params[:company], category: params[:category], department: params[:department],
288
+
289
+ name: params[:name], chargeName: params[:chargeName], other: params[:other], )
290
+
291
+
292
+
293
+ @post.save
294
+
295
+ redirect_to("/customers/#{@post.id}")
296
+
297
+ end
298
+
299
+
300
+
301
+ def show
302
+
303
+ @posts = Post.all
304
+
305
+ @post =Post.find_by(id:params[:id])
306
+
307
+
308
+
309
+ end
310
+
311
+
312
+
313
+ def edit
314
+
315
+ @post = Post.find_by(id:params[:id])
316
+
317
+ end
318
+
319
+
320
+
321
+ def update
322
+
323
+ @post = Post.find_by(id: params[:id])
324
+
325
+ @post.company = params[:company]
326
+
327
+ @post.category = params[:category]
328
+
329
+ @post.department = params[:department]
330
+
331
+ @post.name = params[:name]
332
+
333
+ @post.chargeName = params[:chargeName]
334
+
335
+ @post.other = params[:other]
336
+
337
+
338
+
339
+ if params[:image]
340
+
341
+ @post.image_name ="#{@post.id}.jpg"
342
+
343
+ image = params[:image]
344
+
345
+ File.binwrite("public/images/#{@post.id}.jpg", image.read)
346
+
347
+ end
348
+
349
+
350
+
351
+ @post.save
352
+
353
+ redirect_to("/customers")
354
+
355
+ end
356
+
357
+
358
+
359
+ def destroy
360
+
361
+ @post = Post.find_by(id: params[:id])
362
+
363
+ @post.destroy
364
+
365
+ redirect_to("/customers")
366
+
367
+ end
368
+
369
+
370
+
371
+ def swatch
372
+
373
+ @post = Post.find_by(id:params[:id])
374
+
375
+ end
376
+
377
+
378
+
379
+ def swatch_create
380
+
381
+ @post = Post.find_by(id: params[:id])
382
+
383
+
384
+
385
+ @post.collection = params[:collection]
386
+
387
+ @post.swatch_note = params[:swatch_note]
388
+
389
+ @post.number = params[:number]
390
+
391
+
392
+
393
+ @post.save
394
+
395
+ redirect_to("/customers/#{@post.id}")
396
+
397
+ end
398
+
399
+
400
+
401
+
402
+
403
+ end
330
404
 
331
405
 
332
406
 
@@ -334,150 +408,6 @@
334
408
 
335
409
 
336
410
 
337
- ```Ruby
338
-
339
- class PostsController < ApplicationController
340
-
341
- def index
342
-
343
- @posts = Post.all
344
-
345
- end
346
-
347
-
348
-
349
- def new
350
-
351
- end
352
-
353
-
354
-
355
- def create
356
-
357
- @post = Post.new(company: params[:company], category: params[:category], department: params[:department],
358
-
359
- name: params[:name], chargeName: params[:chargeName], other: params[:other], )
360
-
361
-
362
-
363
- @post.save
364
-
365
- redirect_to("/customers/#{@post.id}")
366
-
367
- end
368
-
369
-
370
-
371
- def show
372
-
373
- @posts = Post.all
374
-
375
- @post =Post.find_by(id:params[:id])
376
-
377
-
378
-
379
- end
380
-
381
-
382
-
383
- def edit
384
-
385
- @post = Post.find_by(id:params[:id])
386
-
387
- end
388
-
389
-
390
-
391
- def update
392
-
393
- @post = Post.find_by(id: params[:id])
394
-
395
- @post.company = params[:company]
396
-
397
- @post.category = params[:category]
398
-
399
- @post.department = params[:department]
400
-
401
- @post.name = params[:name]
402
-
403
- @post.chargeName = params[:chargeName]
404
-
405
- @post.other = params[:other]
406
-
407
-
408
-
409
- if params[:image]
410
-
411
- @post.image_name ="#{@post.id}.jpg"
412
-
413
- image = params[:image]
414
-
415
- File.binwrite("public/images/#{@post.id}.jpg", image.read)
416
-
417
- end
418
-
419
-
420
-
421
- @post.save
422
-
423
- redirect_to("/customers")
424
-
425
- end
426
-
427
-
428
-
429
- def destroy
430
-
431
- @post = Post.find_by(id: params[:id])
432
-
433
- @post.destroy
434
-
435
- redirect_to("/customers")
436
-
437
- end
438
-
439
-
440
-
441
- def swatch
442
-
443
- @post = Post.find_by(id:params[:id])
444
-
445
- end
446
-
447
-
448
-
449
- def swatch_create
450
-
451
- @post = Post.find_by(id: params[:id])
452
-
453
-
454
-
455
- @post.collection = params[:collection]
456
-
457
- @post.swatch_note = params[:swatch_note]
458
-
459
- @post.number = params[:number]
460
-
461
-
462
-
463
- @post.save
464
-
465
- redirect_to("/customers/#{@post.id}")
466
-
467
- end
468
-
469
-
470
-
471
-
472
-
473
- end
474
-
475
-
476
-
477
- ```
478
-
479
-
480
-
481
411
  アドバイスいただければ幸いです。
482
412
 
483
413
  よろしくお願いいたします。