質問編集履歴
8
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -321,4 +321,38 @@
|
|
321
321
|
product.rb
|
322
322
|
```
|
323
323
|
has_many :carts, through: :cart_items
|
324
|
+
```
|
325
|
+
|
326
|
+
### 12/21追記
|
327
|
+
以下のように変更したら、とりあえずカートページに商品を追加することができました。
|
328
|
+
####cart.rb
|
329
|
+
```
|
330
|
+
belongs_to :user
|
331
|
+
has_many :cart_items
|
332
|
+
```
|
333
|
+
|
334
|
+
####cart_item.rb
|
335
|
+
```
|
336
|
+
belongs_to :product
|
337
|
+
belongs_to :cart
|
338
|
+
```
|
339
|
+
|
340
|
+
####product.rb
|
341
|
+
```
|
342
|
+
has_many :cart_items
|
343
|
+
```
|
344
|
+
|
345
|
+
####cart_controller.rb
|
346
|
+
```
|
347
|
+
def index
|
348
|
+
@cart = current_user.cart
|
349
|
+
@cart_items = @cart.cart_items
|
350
|
+
end
|
351
|
+
|
352
|
+
def add_item
|
353
|
+
@cart = current_user.cart
|
354
|
+
@cart_item = @cart.cart_items.build(product_id: params[:product_id])
|
355
|
+
@cart_item.save
|
356
|
+
redirect_to carts_path
|
357
|
+
end
|
324
358
|
```
|
7
関連付けの定義
title
CHANGED
File without changes
|
body
CHANGED
@@ -301,4 +301,24 @@
|
|
301
301
|
上記のように記載すると`undefined method cart_items for 2:Integer`という表示が出てしまいます。
|
302
302
|
おそらく、@user.cart.idがただの整数値だからひっぱってこれていないのかと思っています
|
303
303
|
|
304
|
-
ご教授いただけますと幸いです。
|
304
|
+
ご教授いただけますと幸いです。
|
305
|
+
|
306
|
+
###関連付け
|
307
|
+
user.rb
|
308
|
+
```
|
309
|
+
has_one :cart, dependent: :destroy
|
310
|
+
accepts_nested_attributes_for :cart
|
311
|
+
```
|
312
|
+
|
313
|
+
cart.rb
|
314
|
+
```
|
315
|
+
class Cart < ApplicationRecord
|
316
|
+
belongs_to :user
|
317
|
+
has_many :products, through: :cart_items
|
318
|
+
end
|
319
|
+
```
|
320
|
+
|
321
|
+
product.rb
|
322
|
+
```
|
323
|
+
has_many :carts, through: :cart_items
|
324
|
+
```
|
6
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -263,6 +263,7 @@
|
|
263
263
|
super
|
264
264
|
@user = User.new(configure_sign_up_params)
|
265
265
|
@cart = @user.build_cart
|
266
|
+
@cart.save
|
266
267
|
@user.save
|
267
268
|
end
|
268
269
|
```
|
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -263,7 +263,7 @@
|
|
263
263
|
super
|
264
264
|
@user = User.new(configure_sign_up_params)
|
265
265
|
@cart = @user.build_cart
|
266
|
-
user.save
|
266
|
+
@user.save
|
267
267
|
end
|
268
268
|
```
|
269
269
|
|
4
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -298,5 +298,6 @@
|
|
298
298
|
end
|
299
299
|
```
|
300
300
|
上記のように記載すると`undefined method cart_items for 2:Integer`という表示が出てしまいます。
|
301
|
+
おそらく、@user.cart.idがただの整数値だからひっぱってこれていないのかと思っています
|
301
302
|
|
302
303
|
ご教授いただけますと幸いです。
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -289,13 +289,14 @@
|
|
289
289
|
end
|
290
290
|
|
291
291
|
def add_item
|
292
|
-
user =
|
292
|
+
@user = current_user
|
293
|
-
@cart = user.cart
|
293
|
+
@cart = @user.cart.id
|
294
|
-
@cart_item =
|
294
|
+
@cart_item = @cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank?
|
295
295
|
@cart_item.save
|
296
296
|
redirect_to cart_path
|
297
297
|
end
|
298
298
|
end
|
299
299
|
```
|
300
|
+
上記のように記載すると`undefined method cart_items for 2:Integer`という表示が出てしまいます。
|
300
301
|
|
301
302
|
ご教授いただけますと幸いです。
|
2
new削除
title
CHANGED
File without changes
|
body
CHANGED
@@ -259,15 +259,10 @@
|
|
259
259
|
###追記(2020/12/07)
|
260
260
|
users/registrations_controller.rb
|
261
261
|
```
|
262
|
-
def new
|
263
|
-
@user = User.new
|
264
|
-
@cart = @user.build_cart
|
265
|
-
end
|
266
|
-
|
267
|
-
# POST /resource
|
268
262
|
def create
|
269
263
|
super
|
270
|
-
user = User.new(configure_sign_up_params)
|
264
|
+
@user = User.new(configure_sign_up_params)
|
265
|
+
@cart = @user.build_cart
|
271
266
|
user.save
|
272
267
|
end
|
273
268
|
```
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -254,4 +254,53 @@
|
|
254
254
|
・https://qiita.com/wonder_meet/items/9238d9bedea542ab975b
|
255
255
|
|
256
256
|
【ECサイトのカート機能】
|
257
|
-
・https://qiita.com/kenzo-ta/items/b45994c5f3fdd87b6c50
|
257
|
+
・https://qiita.com/kenzo-ta/items/b45994c5f3fdd87b6c50
|
258
|
+
|
259
|
+
###追記(2020/12/07)
|
260
|
+
users/registrations_controller.rb
|
261
|
+
```
|
262
|
+
def new
|
263
|
+
@user = User.new
|
264
|
+
@cart = @user.build_cart
|
265
|
+
end
|
266
|
+
|
267
|
+
# POST /resource
|
268
|
+
def create
|
269
|
+
super
|
270
|
+
user = User.new(configure_sign_up_params)
|
271
|
+
user.save
|
272
|
+
end
|
273
|
+
```
|
274
|
+
|
275
|
+
これを追加すると、rails cで確認した時に、Cartにuser_idが入っているのですが、
|
276
|
+
|
277
|
+
Product/show.erb
|
278
|
+
```
|
279
|
+
<h1>商品詳細</h1>
|
280
|
+
|
281
|
+
商品名:<%= @product.name %><br>
|
282
|
+
商品説明文:<%= @product.description %><br>
|
283
|
+
|
284
|
+
<%= button_to "カートに入れる", add_item_path(product_id: @product.id) %>
|
285
|
+
```
|
286
|
+
|
287
|
+
商品詳細のカートに入れるを押したあと、Carts#additemにて、user.cartを取得する方法がわかりません。
|
288
|
+
|
289
|
+
```
|
290
|
+
class CartsController < ApplicationController
|
291
|
+
|
292
|
+
def show
|
293
|
+
@cart = Cart.find(params[:id])
|
294
|
+
end
|
295
|
+
|
296
|
+
def add_item
|
297
|
+
user = User.find(params[:id])
|
298
|
+
@cart = user.cart
|
299
|
+
@cart_item = User.cart.cart_items.build(product_id: params[:product_id]) if @cart_item.blank?
|
300
|
+
@cart_item.save
|
301
|
+
redirect_to cart_path
|
302
|
+
end
|
303
|
+
end
|
304
|
+
```
|
305
|
+
|
306
|
+
ご教授いただけますと幸いです。
|