回答編集履歴

1

追記

2019/10/07 00:28

投稿

winterboum
winterboum

スコア23329

test CHANGED
@@ -1,3 +1,33 @@
1
1
  session[:card_id] が残っている? というのは不思議ですが、その前にuserとcartの関係定義ができていないのでは? と思われます。
2
2
 
3
3
  User と Cart の関係定義のところを見せて下さい。 has_* とか belongs_toとかのところです
4
+
5
+ ###
6
+
7
+ 関連定義を追加しましょう
8
+
9
+
10
+
11
+ class User に has_one :cart
12
+
13
+ class Cart に belongs_to :user
14
+
15
+
16
+
17
+ Userのcontroller の create で,成功した後で
18
+
19
+ 成功したuser.cart.create としておきましょう
20
+
21
+
22
+
23
+ そうすると、user.cart でcartが得られますのでsessionに取っておく必要がなくなります。
24
+
25
+
26
+
27
+ あと、Cart,Productとの関係では
28
+
29
+ cartに  has_many :products, through :cart_items
30
+
31
+ productに has_many :carts, through :cart_items
32
+
33
+ も定義しておくほうが良いかと。