質問編集履歴

5

内容に変更があったため

2020/07/05 16:12

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- RailsでECサイトを作成中、商品をカートに追加しようとするとActiveRecord::RecordNotFound in CartsController#add_itemのエラーが出る
1
+ RailsでECサイトを作成中、商品をカートに追加しようとするとNoMethodError in CartsController#add_itemのエラーが出る
test CHANGED
@@ -274,7 +274,7 @@
274
274
 
275
275
  else
276
276
 
277
- render new_product_path
277
+ render @product.current_cart
278
278
 
279
279
  end
280
280
 

4

変更があったため

2020/07/05 16:12

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -76,11 +76,11 @@
76
76
 
77
77
 
78
78
 
79
- %ul
79
+ %ul
80
-
81
- =form_for([@product, @cart_item], url: add_item_product_path, method: :post) do |f|
80
+
82
-
83
- - @products.each do |product|
81
+ - @products.each do |product|
82
+
83
+ =form_for([@product, @cart_item], url: add_item_product_path(product), method: :post) do |f|
84
84
 
85
85
  %li.product
86
86
 
@@ -136,15 +136,11 @@
136
136
 
137
137
  def current_cart
138
138
 
139
- @cart =Cart.find(session[:cart_id])
139
+ @cart = Cart.find_by(id: session[:cart_id]) || Cart.create
140
-
141
- rescue ActiveRecord::RecordNotFound
140
+
142
-
143
- cart = Cart.create
144
-
145
- session[:cart_id] = cart.id
141
+ session[:cart_id] = @cart.id
146
-
142
+
147
- cart
143
+ @cart
148
144
 
149
145
  end
150
146
 
@@ -174,7 +170,7 @@
174
170
 
175
171
  @cart = current_cart
176
172
 
177
- @product =Product.find(params[:product_id])
173
+ @product =Product.find(params[:id])
178
174
 
179
175
  @cart_items = @cart.cart_items.build
180
176
 
@@ -182,7 +178,7 @@
182
178
 
183
179
  if @cart_item.blank?
184
180
 
185
- @cart_item = current_cart.cart_items.build(product_id: params[:product_id], cart_id: current_cart.id)
181
+ @cart_item = current_cart.cart_items.build(product_id: params[:id], cart_id: current_cart.id)
186
182
 
187
183
  end
188
184
 
@@ -254,8 +250,6 @@
254
250
 
255
251
  @products = Product.all
256
252
 
257
- @product = CartItem.new
258
-
259
253
  @cart_item = CartItem.new
260
254
 
261
255
  end
@@ -362,7 +356,7 @@
362
356
 
363
357
  ```
364
358
 
365
- Prefix Verb URI Pattern Controller#Action
359
+ Prefix Verb URI Pattern Controller#Action
366
360
 
367
361
  new_user_session GET /users/sign_in(.:format) users/sessions#new
368
362
 
@@ -404,6 +398,8 @@
404
398
 
405
399
  new_product_cart GET /products/:product_id/carts/new(.:format) carts#new
406
400
 
401
+ product_cart GET /products/:product_id/carts/:id(.:format) carts#show
402
+
407
403
  add_item_product POST /products/:id/add_item(.:format) carts#add_item
408
404
 
409
405
  update_item_product POST /products/:id/update_item(.:format) carts#update_item
@@ -430,12 +426,6 @@
430
426
 
431
427
  PUT /users/:id(.:format) users#update
432
428
 
433
- carts POST /carts(.:format) carts#create
434
-
435
- new_cart GET /carts/new(.:format) carts#new
436
-
437
- cart GET /carts/:id(.:format) carts#show
438
-
439
429
  ```
440
430
 
441
431
 

3

変更があったため

2020/07/05 14:56

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
  resources :carts, only: [:create, :new]
46
46
 
47
- collection do
47
+ member do
48
48
 
49
49
  post '/add_item' => 'carts#add_item'
50
50
 
@@ -76,47 +76,43 @@
76
76
 
77
77
 
78
78
 
79
- %ul
79
+ %ul
80
+
80
-
81
+ =form_for([@product, @cart_item], url: add_item_product_path, method: :post) do |f|
82
+
81
- - @products.each do |product|
83
+ - @products.each do |product|
82
-
84
+
83
- %li.product
85
+ %li.product
84
-
86
+
85
- .pro-main
87
+ .pro-main
86
-
88
+
87
- .pro-left
89
+ .pro-left
88
-
90
+
89
- .pro-image
91
+ .pro-image
90
-
92
+
91
- = image_tag product.image.url, height: "120"
93
+ = image_tag product.image.url, height: "120"
92
-
94
+
93
- .pro-name
95
+ .pro-name
94
-
96
+
95
- = product.name
97
+ = product.name
96
-
98
+
97
- .pro-center
99
+ .pro-center
98
-
100
+
99
- .pro-explain
101
+ .pro-explain
100
-
102
+
101
- = product.explain
103
+ = product.explain
102
-
104
+
103
- .pro-right
105
+ .pro-right
104
-
106
+
105
- .pro-price
107
+ .pro-price
106
-
108
+
107
- = product.price
109
+ = product.price
108
-
110
+
109
- %span
111
+ %span
110
-
112
+
111
-
113
+
112
-
113
- #以下フォーム
114
+
114
-
115
- =form_for([@product, @cart_item], url: add_item_products_path, method: :post) do |f|
116
-
117
- =f.hidden_field :product_id, value: @product.id
115
+ =f.hidden_field :product_id, value: product.id
118
-
119
- /=f.hidden_field :cart_id, value: @cart.id
120
116
 
121
117
  .pro-qua
122
118
 

2

変更があったため

2020/07/05 08:26

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -408,11 +408,11 @@
408
408
 
409
409
  new_product_cart GET /products/:product_id/carts/new(.:format) carts#new
410
410
 
411
- add_item_products POST /products/add_item(.:format) carts#add_item
411
+ add_item_product POST /products/:id/add_item(.:format) carts#add_item
412
-
412
+
413
- update_item_products POST /products/update_item(.:format) carts#update_item
413
+ update_item_product POST /products/:id/update_item(.:format) carts#update_item
414
-
414
+
415
- delete_item_products DELETE /products/delete_item(.:format) carts#delete_item
415
+ delete_item_product DELETE /products/:id/delete_item(.:format) carts#delete_item
416
416
 
417
417
  products GET /products(.:format) products#index
418
418
 

1

ルーティングを載せるため

2020/07/05 08:18

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -364,6 +364,84 @@
364
364
 
365
365
  ```
366
366
 
367
+ ```
368
+
369
+ Prefix Verb URI Pattern Controller#Action
370
+
371
+ new_user_session GET /users/sign_in(.:format) users/sessions#new
372
+
373
+ user_session POST /users/sign_in(.:format) users/sessions#create
374
+
375
+ destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy
376
+
377
+ new_user_password GET /users/password/new(.:format) devise/passwords#new
378
+
379
+ edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
380
+
381
+ user_password PATCH /users/password(.:format) devise/passwords#update
382
+
383
+ PUT /users/password(.:format) devise/passwords#update
384
+
385
+ POST /users/password(.:format) devise/passwords#create
386
+
387
+ cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
388
+
389
+ new_user_registration GET /users/sign_up(.:format) users/registrations#new
390
+
391
+ edit_user_registration GET /users/edit(.:format) users/registrations#edit
392
+
393
+ user_registration PATCH /users(.:format) users/registrations#update
394
+
395
+ PUT /users(.:format) users/registrations#update
396
+
397
+ DELETE /users(.:format) users/registrations#destroy
398
+
399
+ POST /users(.:format) users/registrations#create
400
+
401
+ sign_in GET /sign_in(.:format) users/sessions#new
402
+
403
+ sign_out GET /sign_out(.:format) users/sessions#destroy
404
+
405
+ root GET / products#index
406
+
407
+ product_carts POST /products/:product_id/carts(.:format) carts#create
408
+
409
+ new_product_cart GET /products/:product_id/carts/new(.:format) carts#new
410
+
411
+ add_item_products POST /products/add_item(.:format) carts#add_item
412
+
413
+ update_item_products POST /products/update_item(.:format) carts#update_item
414
+
415
+ delete_item_products DELETE /products/delete_item(.:format) carts#delete_item
416
+
417
+ products GET /products(.:format) products#index
418
+
419
+ POST /products(.:format) products#create
420
+
421
+ new_product GET /products/new(.:format) products#new
422
+
423
+ edit_product GET /products/:id/edit(.:format) products#edit
424
+
425
+ product PATCH /products/:id(.:format) products#update
426
+
427
+ PUT /products/:id(.:format) products#update
428
+
429
+ edit_user GET /users/:id/edit(.:format) users#edit
430
+
431
+ user GET /users/:id(.:format) users#show
432
+
433
+ PATCH /users/:id(.:format) users#update
434
+
435
+ PUT /users/:id(.:format) users#update
436
+
437
+ carts POST /carts(.:format) carts#create
438
+
439
+ new_cart GET /carts/new(.:format) carts#new
440
+
441
+ cart GET /carts/:id(.:format) carts#show
442
+
443
+ ```
444
+
367
445
 
368
446
 
369
447