質問編集履歴
8
更新
test
CHANGED
File without changes
|
test
CHANGED
@@ -396,6 +396,20 @@
|
|
396
396
|
|
397
397
|
```
|
398
398
|
|
399
|
+
```
|
400
|
+
|
401
|
+
(app/controllers/products_controller.rb)一部記載
|
402
|
+
|
403
|
+
def show
|
404
|
+
|
405
|
+
@product_info = Product.find_by(id: params[:id])
|
406
|
+
|
407
|
+
@product_images = @product_info.images
|
408
|
+
|
409
|
+
end
|
410
|
+
|
411
|
+
```
|
412
|
+
|
399
413
|
### 発生している問題・エラーメッセージ
|
400
414
|
|
401
415
|
```
|
7
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -356,8 +356,6 @@
|
|
356
356
|
|
357
357
|
end
|
358
358
|
|
359
|
-
|
360
|
-
|
361
359
|
```
|
362
360
|
|
363
361
|
```
|
@@ -404,28 +402,30 @@
|
|
404
402
|
|
405
403
|
以下のようなエラーとなりました。
|
406
404
|
|
405
|
+
cartテーブルにはデータ(current_user.id=user_id)が入りました。
|
406
|
+
|
407
|
-
|
407
|
+
ただ、cart_itemsのテーブルにはデータが登録されませんでした。
|
408
|
-
|
409
|
-
|
410
|
-
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
-
|
411
|
+
Validation failed: Product must exist
|
412
412
|
|
413
413
|
Extracted source (around line #9):
|
414
414
|
|
415
|
-
7
|
415
|
+
7 def create
|
416
|
-
|
416
|
+
|
417
|
-
8 c
|
417
|
+
8 @cart_item = @cart.cart_items.build(product_id: session[:product_id]) if @cart_item.blank?
|
418
|
-
|
418
|
+
|
419
|
-
9
|
419
|
+
9 if @cart_item.save!
|
420
|
-
|
420
|
+
|
421
|
-
10 current_cart
|
421
|
+
10 redirect_to current_cart
|
422
422
|
|
423
423
|
11 else
|
424
424
|
|
425
|
-
12
|
425
|
+
12 redirect_to products_path
|
426
|
-
|
427
|
-
|
428
|
-
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
-
```
|
429
|
+
```
|
430
|
-
|
430
|
+
|
431
|
-
|
431
|
+
if @cart_item.save!の「!」を外すと、elseに回って、products_path(商品一覧画面)にいきます。
|
6
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -427,3 +427,5 @@
|
|
427
427
|
|
428
428
|
|
429
429
|
```
|
430
|
+
|
431
|
+
まだ、session[:cart_id]には何もないはずなのに、if文でelseに行っていないところが変な感じがします。
|
5
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -408,22 +408,22 @@
|
|
408
408
|
|
409
409
|
|
410
410
|
|
411
|
-
|
411
|
+
undefined method `id' for nil:NilClass
|
412
412
|
|
413
413
|
Extracted source (around line #9):
|
414
414
|
|
415
|
-
7 def create
|
416
|
-
|
417
|
-
8 @cart_item = @cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank?
|
418
|
-
|
419
|
-
|
415
|
+
7 if session[:cart_id]
|
416
|
+
|
420
|
-
|
417
|
+
8 current_cart = Cart.find_by(id: session[:cart_id])
|
418
|
+
|
419
|
+
9 session[:cart_id] = current_cart.id
|
420
|
+
|
421
|
-
10
|
421
|
+
10 current_cart
|
422
|
-
|
422
|
+
|
423
|
-
11
|
423
|
+
11 else
|
424
|
-
|
424
|
+
|
425
|
-
12
|
425
|
+
12 current_cart = Cart.create(user_id: current_user.id)
|
426
|
-
|
427
|
-
|
428
|
-
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
-
```
|
429
|
+
```
|
4
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -270,7 +270,7 @@
|
|
270
270
|
|
271
271
|
def create
|
272
272
|
|
273
|
-
@cart_item = @cart.cart_items.build(product_id:
|
273
|
+
@cart_item = @cart.cart_items.build(product_id: session[:product_id]) if @cart_item.blank? ←★変更点(session[:product_id])
|
274
274
|
|
275
275
|
if @cart_item.save! ←★変更点
|
276
276
|
|
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -394,7 +394,7 @@
|
|
394
394
|
|
395
395
|
(app/models/user.rb)一部記載
|
396
396
|
|
397
|
-
|
397
|
+
has_many :carts
|
398
398
|
|
399
399
|
```
|
400
400
|
|
2
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -272,7 +272,7 @@
|
|
272
272
|
|
273
273
|
@cart_item = @cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank?
|
274
274
|
|
275
|
-
if @cart_item.save!
|
275
|
+
if @cart_item.save! ←★変更点
|
276
276
|
|
277
277
|
redirect_to current_cart
|
278
278
|
|
@@ -397,3 +397,33 @@
|
|
397
397
|
|
398
398
|
|
399
399
|
```
|
400
|
+
|
401
|
+
### 発生している問題・エラーメッセージ
|
402
|
+
|
403
|
+
```
|
404
|
+
|
405
|
+
以下のようなエラーとなりました。
|
406
|
+
|
407
|
+
DBのテーブルにもデータは登録されませんでした。
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
Validation failed: Product must exist
|
412
|
+
|
413
|
+
Extracted source (around line #9):
|
414
|
+
|
415
|
+
7 def create
|
416
|
+
|
417
|
+
8 @cart_item = @cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank?
|
418
|
+
|
419
|
+
9 if @cart_item.save!
|
420
|
+
|
421
|
+
10 redirect_to current_cart
|
422
|
+
|
423
|
+
11 else
|
424
|
+
|
425
|
+
12 redirect_to products_path
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
```
|
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,7 +54,7 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
-
```
|
57
|
+
```
|
58
58
|
|
59
59
|
(app/controllers/carts_controller)
|
60
60
|
|
@@ -225,3 +225,175 @@
|
|
225
225
|
ruby 2.6.6
|
226
226
|
|
227
227
|
rails '~> 5.2.4', '>= 5.2.4.3'
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
### 編集後/最新コード
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
(app/vies/products/show.html.erb)
|
236
|
+
|
237
|
+
<div class="cart">
|
238
|
+
|
239
|
+
<%= form_for(:session, url: cart_items_path) do |f|%>
|
240
|
+
|
241
|
+
<%= f.label :quantity, "数量"%>
|
242
|
+
|
243
|
+
<%= f.select :quantity, [1,2,3,4,5,6,7,8,9,10]%>
|
244
|
+
|
245
|
+
<%= f.hidden_field :product_id, value: @product_info.id %> ←★変更点
|
246
|
+
|
247
|
+
<%= f.submit "カートに追加", class:"btn cart_btn" %>
|
248
|
+
|
249
|
+
<% end %>
|
250
|
+
|
251
|
+
</div>
|
252
|
+
|
253
|
+
```
|
254
|
+
|
255
|
+
```
|
256
|
+
|
257
|
+
(app/controllers/cart_items_controller)←★変更点(carts_controller.rbの内容を全てcart_items_controller.rb)に移動
|
258
|
+
|
259
|
+
class CartItemsController < ApplicationController
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
before_action :set_line_item, only: [:create, :destroy]
|
264
|
+
|
265
|
+
before_action :set_user
|
266
|
+
|
267
|
+
before_action :set_cart
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
def create
|
272
|
+
|
273
|
+
@cart_item = @cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank?
|
274
|
+
|
275
|
+
if @cart_item.save!
|
276
|
+
|
277
|
+
redirect_to current_cart
|
278
|
+
|
279
|
+
else
|
280
|
+
|
281
|
+
redirect_to products_path
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
def destroy
|
290
|
+
|
291
|
+
@cart.destroy
|
292
|
+
|
293
|
+
redirect_to current_cart
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
private
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
def set_user
|
304
|
+
|
305
|
+
@user = current_user
|
306
|
+
|
307
|
+
end
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
def set_line_item
|
312
|
+
|
313
|
+
@cart_item = current_cart.cart_items.find_by(product_id: params[:product_id])
|
314
|
+
|
315
|
+
end
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
def set_cart
|
320
|
+
|
321
|
+
@cart = current_cart
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
end
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
```
|
330
|
+
|
331
|
+
```
|
332
|
+
|
333
|
+
(app/controllers/application_controller)一部記載
|
334
|
+
|
335
|
+
def current_cart
|
336
|
+
|
337
|
+
if session[:cart_id]
|
338
|
+
|
339
|
+
current_cart = Cart.find_by(id: session[:cart_id])
|
340
|
+
|
341
|
+
session[:cart_id] = current_cart.id
|
342
|
+
|
343
|
+
current_cart
|
344
|
+
|
345
|
+
else
|
346
|
+
|
347
|
+
current_cart = Cart.create(user_id: current_user.id)
|
348
|
+
|
349
|
+
session[:cart_id] = current_cart.id
|
350
|
+
|
351
|
+
current_cart = Cart.find_by(id: session[:cart_id])
|
352
|
+
|
353
|
+
current_cart
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
end
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
```
|
362
|
+
|
363
|
+
```
|
364
|
+
|
365
|
+
(app/models/cart_item.rb)
|
366
|
+
|
367
|
+
class CartItem < ApplicationRecord
|
368
|
+
|
369
|
+
belongs_to :product
|
370
|
+
|
371
|
+
belongs_to :cart
|
372
|
+
|
373
|
+
belongs_to :user
|
374
|
+
|
375
|
+
end
|
376
|
+
|
377
|
+
```
|
378
|
+
|
379
|
+
```
|
380
|
+
|
381
|
+
(app/models/cart.rb)
|
382
|
+
|
383
|
+
class Cart < ApplicationRecord
|
384
|
+
|
385
|
+
has_many :cart_items
|
386
|
+
|
387
|
+
belongs_to :user ←★変更点
|
388
|
+
|
389
|
+
end
|
390
|
+
|
391
|
+
```
|
392
|
+
|
393
|
+
```
|
394
|
+
|
395
|
+
(app/models/user.rb)一部記載
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
```
|