質問編集履歴

1

大幅に修正いたしました

2018/05/10 22:11

投稿

begenner
begenner

スコア79

test CHANGED
@@ -1 +1 @@
1
- Railsのカート機能の実装方法わかりません
1
+ 予想外のところで param is missing or the value is empty: 発生する
test CHANGED
@@ -1,8 +1,8 @@
1
1
  現在ECサイトを作成しています。
2
2
 
3
- 商品ページを個別に作成しそこからカートに加える機能を実装したいです。
3
+ 商品ページを個別に作成しそこからカートに加える機能を実装したいです。
4
-
4
+
5
- 今参考にしているのはQiitaの
5
+ 今参考にしているのは
6
6
 
7
7
 
8
8
 
@@ -10,13 +10,21 @@
10
10
 
11
11
 
12
12
 
13
- すがViewに関する部が抜落ちいるめイメージがわきません
13
+ でViewは自で付足しみました。
14
-
15
- 商品ページの中でform_tagを利用してカートに送るという方法だと思いますが、
14
+
16
-
15
+
16
+
17
- 商品IDとカードID取得の仕方がわかりません。
17
+ ですが、カートに入れたい商品を保存する時に
18
-
18
+
19
+
20
+
19
- carts_controllerに関しては記述が合っているかわからずどうすれば良いかわかりません(m_ _m)
21
+ param is missing or the value is empty: cartitem
22
+
23
+ ![イメージ説明](dc8bc1b672d9b86044fb1c2bffc3c648.png)
24
+
25
+ ![イメージ説明](a71675f9e1fc3b3a3137cf5b7b2d88dd.png)
26
+
27
+ のエラーが発生し、原因がわからず困っています。
20
28
 
21
29
  わかる方いらっしゃいましたらお手数ですがご教授いだたきますようよろしくお願い申し上げます(m_ _m)
22
30
 
@@ -24,7 +32,7 @@
24
32
 
25
33
  ###期待する動作
26
34
 
27
-
35
+ カートに入れるを押すとその商品がユーザと紐付けられて保存される
28
36
 
29
37
  ###動作環境
30
38
 
@@ -50,50 +58,36 @@
50
58
 
51
59
 
52
60
 
53
- - Userのレコドが作成された時に同時にCartレコードが作成されます
61
+ - ト機能(カートに入る、カートの中身を表示する)を実装するめの記述はCarts_controllerを作成して書いています
54
62
 
55
63
  - 商品ページは個別に作成しています
56
64
 
57
65
 
58
66
 
67
+ ###試したこと
68
+
69
+ @cartitem =Cartitem.new#(cartitem_params)とするとエラーが出ずに画面遷移しました(レコードの登録はできていません)。
70
+
71
+
72
+
59
73
  ###該当のソースコード(必要だと思う部分だけ)
60
74
 
61
75
  db/schema.rb
62
76
 
63
77
  ```ruby
64
78
 
65
- #cartテーブル
79
+ #cartitemテーブル
66
-
80
+
67
- create_table "carts", force: :cascade do |t|
81
+ create_table "cartitems", force: :cascade do |t|
82
+
83
+ t.integer "quantity", limit: 4
84
+
85
+ t.integer "item_id", limit: 4
68
86
 
69
87
  t.integer "user_id", limit: 4
70
88
 
71
89
  end
72
90
 
73
- add_index "carts", ["user_id"], name: "fk_rails_ea59a35211", using: :btree()
74
-
75
-
76
-
77
- #cartitemテーブル
78
-
79
- create_table "cartitems", force: :cascade do |t|
80
-
81
- t.integer "quantity", limit: 4
82
-
83
- t.integer "item_id", limit: 4
84
-
85
- t.integer "cart_id", limit: 4
86
-
87
- end
88
-
89
-
90
-
91
- add_index "cartitems", ["cart_id"], name: "index_cartitems_on_cart_id", using: :btree
92
-
93
- add_index "cartitems", ["item_id"], name: "index_cartitems_on_item_id", using: :btree
94
-
95
-
96
-
97
91
  #商品テーブル
98
92
 
99
93
  create_table "items", force: :cascade do |t|
@@ -114,25 +108,17 @@
114
108
 
115
109
  ```ruby
116
110
 
117
- #cart
118
-
119
- resources :carts, only: [:show]
120
-
121
-
122
-
123
- #商品
124
-
125
111
  resources :items do
126
112
 
127
113
  member do
128
114
 
129
115
  get 'preview'#個別ページ
130
116
 
117
+ post '/add_item' => 'carts#add_item'#カートへ追加する
118
+
131
119
  end
132
120
 
133
- #カートへ追加する(previewページの中に埋め込みたい)
121
+
134
-
135
- post '/add_item' => 'carts#add_item'
136
122
 
137
123
  ```
138
124
 
@@ -150,39 +136,67 @@
150
136
 
151
137
  def add_item
152
138
 
139
+
140
+
141
+ if @cartitem.blank?
142
+
153
143
  #カートに同じ商品がない場合
154
144
 
145
+ #↓ ↓ ↓ ↓ ↓これだとエラーが発生する
146
+
147
+ @cartitem =Cartitem.new(cartitem_params)
148
+
149
+ current_user.cartitems.build(cartitem_params)
150
+
151
+ #↓ ↓ ↓ ↓ ↓これだとエラーが発生せずレコードは作成されるが、数量・商品IDが保存されない
152
+
153
+ @cartitem =Cartitem.new#(cartitem_params)
154
+
155
- if @cartItem.blank?
155
+ @cartitem.save
156
+
156
-
157
+ current_user.cartitems.build
158
+
157
- @cartItem = @cart.cartitems.build(item_id: params[:item_id])
159
+ @item =Item.find_by(id: @cartItem.id)
160
+
161
+ redirect_to :cart_path, success:"#{@item.itemName}がカートに追加されました"
162
+
163
+ else
164
+
165
+ #カートに同じ商品がある場合
166
+
167
+ @cartitem.quantity += params[:quantity].to_i
168
+
169
+ @cartitem.save
170
+
171
+ @item =Item.find_by(id: @cartItem.id)
172
+
173
+ redirect_to :cart_path, success:"#{@item.itemName}がカートに追加されました"
158
174
 
159
175
  end
160
176
 
161
- #カートに同じ商品がある場合
162
-
163
- @cartItem.quantity += params[:quantity].to_i
164
-
165
- @cartItem.save
166
-
167
- @item =Item.find_by(id: @cartItem.id)
168
-
169
- redirect_to :cart_path, success:"#{@item.itemName}がカートに追加されました"
170
-
171
-
172
-
173
177
  end
174
178
 
175
179
 
176
180
 
177
181
  private
178
182
 
179
-
180
-
181
- def set_cartItems
183
+ def set_cartitems
182
-
184
+
183
- @cart = Cart.find_by(user_id: current_user.id)
185
+ # @cartitem = Cartitem.new
184
-
186
+
185
- @cartItem = @cart.cartitems.find_by(item_id: params[:item_id])
187
+ @cartitem = current_user.cartitems.find_by(item_id: params[:item_id])
188
+
189
+ binding.pry
190
+
191
+ end
192
+
193
+
194
+
195
+ def cartitem_params
196
+
197
+ #ここの部分でエラーが発生する?
198
+
199
+ params.require(:cartitem).permit(:quantity, :item_id, :user_id)
186
200
 
187
201
  end
188
202
 
@@ -190,7 +204,7 @@
190
204
 
191
205
  ```
192
206
 
193
- app/controllers/carts_controller.rb
207
+ app/controllers/items_controller.rb
194
208
 
195
209
  ```ruby
196
210
 
@@ -212,21 +226,13 @@
212
226
 
213
227
  ```ruby
214
228
 
215
- <!-- form_for は使っていません-->
216
-
217
- <%= @item.itemName %>
218
-
219
229
  .
220
230
 
221
231
  <%= form_tag("/add_item", method: "post") do %>
222
232
 
223
- <%= number_field :quantity %>
233
+ <%= text_field_tag :quantity %>
224
-
225
- <!-- ↓↓↓ここに自動的に値が入るようにしたい-->
234
+
226
-
227
- <%= hidden_field :item_id %>
235
+ <%= hidden_field_tag :item_id, @item.id %>
228
-
229
- <%= hidden_field :cart_id %>
230
236
 
231
237
  <%= submit_tag("カートに入れる") %>
232
238