質問編集履歴
10
ER図訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -284,7 +284,7 @@
|
|
284
284
|
|
285
285
|
|
286
286
|
|
287
|
-
![Model ER図改正](
|
287
|
+
![Model ER図改正]![イメージ説明](10b25a872a3fcb5d103c05ae88a44e33.png)
|
288
288
|
|
289
289
|
|
290
290
|
|
9
ER図改正
test
CHANGED
File without changes
|
test
CHANGED
@@ -284,7 +284,7 @@
|
|
284
284
|
|
285
285
|
|
286
286
|
|
287
|
-
![
|
287
|
+
![Model ER図改正](3787861437a3d620e25e2a22b6356e18.png)
|
288
288
|
|
289
289
|
|
290
290
|
|
8
teisei
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
Ruby on Railsでショッピングカート機能を実装しています。
|
2
2
|
|
3
|
-
商品一覧画面から、商品の個数を選択してcart
|
3
|
+
商品一覧画面から、商品の個数を選択してcartテーブルに保存したい状況です。
|
4
|
-
|
4
|
+
|
5
|
-
※cart
|
5
|
+
※cartテーブルはsessionで仮に商品を保存するためのテーブルとなってます。
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
-
そして商品一覧indexのコントローラーで、cart
|
9
|
+
そして商品一覧indexのコントローラーで、cart.newを記述して、cartのコントローラーで
|
10
10
|
|
11
11
|
createメソッドに飛ばしています。
|
12
12
|
|
7
cart_itemをcartに変更し、前コードを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,7 +34,7 @@
|
|
34
34
|
|
35
35
|
@boxlunches = Boxlunch.all
|
36
36
|
|
37
|
-
@cart
|
37
|
+
@cart = Cart.new
|
38
38
|
|
39
39
|
end
|
40
40
|
|
@@ -48,6 +48,8 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
+
|
52
|
+
|
51
53
|
```
|
52
54
|
|
53
55
|
-------------------------------------------------
|
@@ -68,11 +70,21 @@
|
|
68
70
|
|
69
71
|
|
70
72
|
|
73
|
+
= render 'layouts/header'
|
74
|
+
|
75
|
+
= render "./template/menu_selections"
|
76
|
+
|
77
|
+
.menu-container
|
78
|
+
|
79
|
+
%h2 Boxlunch Menu
|
80
|
+
|
71
|
-
.menu-contents
|
81
|
+
.menu-contents
|
72
82
|
|
73
83
|
- @boxlunches.each do |boxlunch|
|
74
84
|
|
85
|
+
#cart_index_pathでcartのcreateに飛びます
|
86
|
+
|
75
|
-
= form_for @cart
|
87
|
+
= form_for @cart, url: cart_index_path(boxlunch_id: boxlunch.id), method: :post do |f|
|
76
88
|
|
77
89
|
.menu-package
|
78
90
|
|
@@ -104,6 +116,8 @@
|
|
104
116
|
|
105
117
|
=f.submit "カートの中に入れる"
|
106
118
|
|
119
|
+
= render 'layouts/footer'
|
120
|
+
|
107
121
|
```
|
108
122
|
|
109
123
|
|
@@ -128,77 +142,75 @@
|
|
128
142
|
|
129
143
|
ターミナル
|
130
144
|
|
131
|
-
Started POST "/cart
|
132
|
-
|
133
|
-
Processing by Cart
|
134
|
-
|
135
|
-
Parameters: {"utf8"=>"✓", "authenticity_token"=>"
|
136
|
-
|
137
|
-
Order Load (0
|
145
|
+
Started POST "/cart?boxlunch_id=1" for ::1 at 2020-01-13 23:49:36 +0900
|
146
|
+
|
147
|
+
Processing by CartController#create as HTML
|
148
|
+
|
149
|
+
Parameters: {"utf8"=>"✓", "authenticity_token"=>"O5g+me6pcK+uueZYWtdRBmkFdj+TzXqkyZRyw+q5KvkcqQ3wGLzM05qi79LfoOL9vd7aI54B78hSBNdXYOML2A==", "cart"=>{"boxlunch_quantity"=>"1"}, "commit"=>"カートの中に入れる", "boxlunch_id"=>"1"}
|
150
|
+
|
151
|
+
Order Load (2.0ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" IS NULL LIMIT $1 [["LIMIT", 1]]
|
138
152
|
|
139
153
|
↳ app/controllers/application_controller.rb:16
|
140
154
|
|
141
|
-
(0.
|
155
|
+
(0.8ms) BEGIN
|
142
156
|
|
143
157
|
↳ app/controllers/application_controller.rb:17
|
144
158
|
|
159
|
+
(1.1ms) ROLLBACK
|
160
|
+
|
161
|
+
↳ app/controllers/application_controller.rb:17
|
162
|
+
|
163
|
+
Boxlunch Load (0.7ms) SELECT "boxlunches".* FROM "boxlunches" WHERE "boxlunches"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
164
|
+
|
165
|
+
↳ app/controllers/cart_controller.rb:4
|
166
|
+
|
167
|
+
Order Load (1.6ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" IS NULL LIMIT $1 [["LIMIT", 1]]
|
168
|
+
|
169
|
+
↳ app/controllers/application_controller.rb:16
|
170
|
+
|
171
|
+
(1.8ms) BEGIN
|
172
|
+
|
173
|
+
↳ app/controllers/application_controller.rb:17
|
174
|
+
|
145
175
|
(0.4ms) ROLLBACK
|
146
176
|
|
147
177
|
↳ app/controllers/application_controller.rb:17
|
148
178
|
|
149
|
-
|
150
|
-
|
151
|
-
↳ app/controllers/cart_
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
↳ app/controllers/
|
156
|
-
|
157
|
-
(
|
158
|
-
|
159
|
-
↳ app/controllers/
|
160
|
-
|
161
|
-
(0.
|
162
|
-
|
163
|
-
↳ app/controllers/
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
178
|
-
|
179
|
-
↳ app/controllers/cart_items_controller.rb:8
|
180
|
-
|
181
|
-
Redirected to http://localhost:3000/cart_items/1
|
182
|
-
|
183
|
-
Completed 302 Found in 33ms (ActiveRecord: 5.3ms)
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
Started GET "/cart_items/1" for ::1 at 2020-01-12 23:54:40 +0900
|
190
|
-
|
191
|
-
Processing by CartItemsController#show as HTML
|
179
|
+
(1.0ms) BEGIN
|
180
|
+
|
181
|
+
↳ app/controllers/cart_controller.rb:6
|
182
|
+
|
183
|
+
Cart Create (0.5ms) INSERT INTO "carts" ("boxlunch_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["boxlunch_id", 1], ["created_at", "2020-01-13 14:49:36.819147"], ["updated_at", "2020-01-13 14:49:36.819147"]]
|
184
|
+
|
185
|
+
↳ app/controllers/cart_controller.rb:6
|
186
|
+
|
187
|
+
(1.9ms) COMMIT
|
188
|
+
|
189
|
+
↳ app/controllers/cart_controller.rb:6
|
190
|
+
|
191
|
+
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
192
|
+
|
193
|
+
↳ app/controllers/cart_controller.rb:7
|
194
|
+
|
195
|
+
Redirected to http://localhost:3000/cart/1
|
196
|
+
|
197
|
+
Completed 302 Found in 46ms (ActiveRecord: 12.1ms)
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
Started GET "/cart/1" for ::1 at 2020-01-13 23:49:36 +0900
|
204
|
+
|
205
|
+
Processing by CartController#show as HTML
|
192
206
|
|
193
207
|
Parameters: {"id"=>"1"}
|
194
208
|
|
195
|
-
Rendering cart
|
209
|
+
Rendering cart/show.html.haml within layouts/application
|
196
|
-
|
210
|
+
|
197
|
-
Rendered cart
|
211
|
+
Rendered cart/show.html.haml within layouts/application (1.9ms)
|
198
|
-
|
212
|
+
|
199
|
-
Completed 200 OK in
|
213
|
+
Completed 200 OK in 63ms (Views: 60.4ms | ActiveRecord: 0.0ms)
|
200
|
-
|
201
|
-
|
202
214
|
|
203
215
|
```
|
204
216
|
|
@@ -268,17 +280,19 @@
|
|
268
280
|
|
269
281
|
```
|
270
282
|
|
271
|
-
|
272
|
-
|
273
283
|
--------------------------------------------------------------------------
|
274
284
|
|
275
285
|
|
276
286
|
|
287
|
+
![model ER図](5a80c9978d0b6437424095b58b62462a.png)
|
288
|
+
|
289
|
+
|
290
|
+
|
277
|
-
カート
|
291
|
+
カートのモデルコード
|
278
|
-
|
292
|
+
|
279
|
-
```ruby
|
293
|
+
```ruby
|
280
|
-
|
294
|
+
|
281
|
-
class Cart
|
295
|
+
class Cart < ApplicationRecord
|
282
296
|
|
283
297
|
belongs_to :order
|
284
298
|
|
@@ -290,17 +304,17 @@
|
|
290
304
|
|
291
305
|
def add_boxlunch(boxlunch_id)
|
292
306
|
|
293
|
-
cart
|
307
|
+
cart.find_or_initialize_by(boxlunch_id: boxlunch_id)
|
294
|
-
|
308
|
+
|
295
|
-
end
|
309
|
+
end
|
296
310
|
|
297
311
|
|
298
312
|
|
299
|
-
def add_single_menu(single_menu_id)
|
313
|
+
def add_single_menu(single_menu_id)
|
300
|
-
|
314
|
+
|
301
|
-
cart
|
315
|
+
cart.find_or_initialize_by(single_menu_id: single_menu_id)
|
302
|
-
|
316
|
+
|
303
|
-
end
|
317
|
+
end
|
304
318
|
|
305
319
|
end
|
306
320
|
|
@@ -310,55 +324,55 @@
|
|
310
324
|
|
311
325
|
|
312
326
|
|
327
|
+
|
328
|
+
|
313
329
|
-------------------------------------------------------------
|
314
330
|
|
315
331
|
|
316
332
|
|
317
|
-
カート
|
333
|
+
カートのコントローラーのコード
|
318
|
-
|
334
|
+
|
319
|
-
```ruby
|
335
|
+
```ruby
|
320
|
-
|
336
|
+
|
321
|
-
class Cart
|
337
|
+
class CartController < ApplicationController
|
322
|
-
|
323
|
-
|
324
|
-
|
338
|
+
|
325
|
-
def create
|
339
|
+
def create
|
326
|
-
|
340
|
+
|
327
|
-
cart = current_cart
|
341
|
+
cart = current_cart
|
328
|
-
|
342
|
+
|
329
|
-
boxlunch = Boxlunch.find(params[:boxlunch_id])
|
343
|
+
boxlunch = Boxlunch.find(params[:boxlunch_id])
|
330
|
-
|
344
|
+
|
331
|
-
@cart
|
345
|
+
@cart = current_cart.add_boxlunch(boxlunch.id)
|
332
|
-
|
333
|
-
|
346
|
+
|
334
|
-
|
335
|
-
if @cart
|
347
|
+
if @cart.save
|
336
|
-
|
348
|
+
|
337
|
-
redirect_to cart_
|
349
|
+
redirect_to cart_path(current_user)
|
338
|
-
|
350
|
+
|
339
|
-
else
|
351
|
+
else
|
340
|
-
|
352
|
+
|
341
|
-
render :new
|
353
|
+
render :new
|
354
|
+
|
355
|
+
end
|
342
356
|
|
343
357
|
end
|
344
358
|
|
359
|
+
|
360
|
+
|
361
|
+
private
|
362
|
+
|
363
|
+
def cart_params
|
364
|
+
|
365
|
+
params.require(:cart).permit(:boxlunch_quantity);
|
366
|
+
|
345
|
-
end
|
367
|
+
end
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
368
|
+
|
350
|
-
|
351
|
-
def cart_item_params
|
352
|
-
|
353
|
-
params.require(:cart_item).permit(:boxlunch_quantity);
|
354
|
-
|
355
|
-
end
|
369
|
+
end
|
356
|
-
|
357
|
-
|
370
|
+
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
```
|
371
|
+
```
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
|
362
376
|
|
363
377
|
-----------------------------------------------------------------
|
364
378
|
|
@@ -371,7 +385,3 @@
|
|
371
385
|
|
372
386
|
|
373
387
|
宜しくお願いいたします。
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
追記:![イメージ説明](63ac201d7b440b2af5dd6022837710d1.png)
|
6
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,33 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
|
27
|
+
```ruby
|
28
|
+
|
29
|
+
index前のコントローラーになります。
|
30
|
+
|
31
|
+
class BoxlunchesController < ApplicationController
|
32
|
+
|
33
|
+
def index
|
34
|
+
|
35
|
+
@boxlunches = Boxlunch.all
|
36
|
+
|
37
|
+
@cart_item = CartItem.new
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
def show
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```
|
52
|
+
|
53
|
+
-------------------------------------------------
|
28
54
|
|
29
55
|
課題:
|
30
56
|
|
@@ -34,6 +60,8 @@
|
|
34
60
|
|
35
61
|
|
36
62
|
|
63
|
+
|
64
|
+
|
37
65
|
```Ruby
|
38
66
|
|
39
67
|
index.html.haml
|
5
画像追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -343,3 +343,7 @@
|
|
343
343
|
|
344
344
|
|
345
345
|
宜しくお願いいたします。
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
追記:![イメージ説明](63ac201d7b440b2af5dd6022837710d1.png)
|
4
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -186,6 +186,66 @@
|
|
186
186
|
|
187
187
|
|
188
188
|
|
189
|
+
|
190
|
+
|
191
|
+
アプリケーションコントローラーのコード
|
192
|
+
|
193
|
+
※current_cartを定義しています。
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
|
197
|
+
class ApplicationController < ActionController::Base
|
198
|
+
|
199
|
+
protect_from_forgery with: :exception
|
200
|
+
|
201
|
+
before_action :authenticate_user!
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
def after_sign_in_path_for(resource)
|
208
|
+
|
209
|
+
root_path # ログイン後に遷移するpathを設定
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
def after_sign_out_path_for(resource)
|
216
|
+
|
217
|
+
root_path # ログアウト後に遷移するpathを設定
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
private
|
224
|
+
|
225
|
+
def current_cart
|
226
|
+
|
227
|
+
@current_cart = Order.find_by(id: session[:order_id])
|
228
|
+
|
229
|
+
@current_cart = Order.create unless @current_cart
|
230
|
+
|
231
|
+
session[:order_id] = @current_cart.id
|
232
|
+
|
233
|
+
@current_cart
|
234
|
+
|
235
|
+
end
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
--------------------------------------------------------------------------
|
246
|
+
|
247
|
+
|
248
|
+
|
189
249
|
カートアイテムのモデルコード
|
190
250
|
|
191
251
|
```ruby
|
@@ -272,67 +332,7 @@
|
|
272
332
|
|
273
333
|
```
|
274
334
|
|
275
|
-
|
276
|
-
|
277
|
-
----------------------------------
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
アプリケーションコントローラーのコード
|
282
|
-
|
283
|
-
※current_cartを定義しています。
|
284
|
-
|
285
|
-
```ruby
|
286
|
-
|
287
|
-
class ApplicationController < ActionController::Base
|
288
|
-
|
289
|
-
protect_from_forgery with: :exception
|
290
|
-
|
291
|
-
before_action :authenticate_user!
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
def after_sign_in_path_for(resource)
|
298
|
-
|
299
|
-
root_path # ログイン後に遷移するpathを設定
|
300
|
-
|
301
|
-
end
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
def after_sign_out_path_for(resource)
|
306
|
-
|
307
|
-
root_path # ログアウト後に遷移するpathを設定
|
308
|
-
|
309
|
-
end
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
private
|
314
|
-
|
315
|
-
def current_cart
|
316
|
-
|
317
|
-
@current_cart = Order.find_by(id: session[:order_id])
|
318
|
-
|
319
|
-
@current_cart = Order.create unless @current_cart
|
320
|
-
|
321
|
-
session[:order_id] = @current_cart.id
|
322
|
-
|
323
|
-
@current_cart
|
324
|
-
|
325
|
-
end
|
326
|
-
|
327
|
-
end
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
```
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
-----------------------------------------------------------------
|
335
|
+
-----------------------------------------------------------------
|
336
336
|
|
337
337
|
|
338
338
|
|
3
訂正
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,7 +92,7 @@
|
|
92
92
|
|
93
93
|
エラー内容
|
94
94
|
|
95
|
-
|
95
|
+
ブラウザーではエラーは表示されていないので
|
96
96
|
|
97
97
|
ターミナルでの記載になります。
|
98
98
|
|
2
code修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
現状:
|
2
|
-
|
3
1
|
Ruby on Railsでショッピングカート機能を実装しています。
|
4
2
|
|
5
3
|
商品一覧画面から、商品の個数を選択してcart_itemテーブルに保存したい状況です。
|
@@ -36,13 +34,13 @@
|
|
36
34
|
|
37
35
|
|
38
36
|
|
39
|
-
|
37
|
+
```Ruby
|
40
|
-
|
38
|
+
|
41
|
-
|
39
|
+
index.html.haml
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
|
41
|
+
|
42
|
+
|
45
|
-
.menu-contents
|
43
|
+
.menu-contents
|
46
44
|
|
47
45
|
- @boxlunches.each do |boxlunch|
|
48
46
|
|
@@ -78,6 +76,14 @@
|
|
78
76
|
|
79
77
|
=f.submit "カートの中に入れる"
|
80
78
|
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
81
87
|
|
82
88
|
|
83
89
|
----------------------------------------------------------------------------
|
@@ -90,7 +96,9 @@
|
|
90
96
|
|
91
97
|
ターミナルでの記載になります。
|
92
98
|
|
93
|
-
|
99
|
+
```ruby
|
100
|
+
|
101
|
+
ターミナル
|
94
102
|
|
95
103
|
Started POST "/cart_items?boxlunch_id=2" for ::1 at 2020-01-12 23:54:40 +0900
|
96
104
|
|
@@ -164,6 +172,12 @@
|
|
164
172
|
|
165
173
|
|
166
174
|
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
|
180
|
+
|
167
181
|
|
168
182
|
|
169
183
|
|
@@ -174,6 +188,8 @@
|
|
174
188
|
|
175
189
|
カートアイテムのモデルコード
|
176
190
|
|
191
|
+
```ruby
|
192
|
+
|
177
193
|
class CartItem < ApplicationRecord
|
178
194
|
|
179
195
|
belongs_to :order
|
@@ -188,117 +204,131 @@
|
|
188
204
|
|
189
205
|
cart_items.find_or_initialize_by(boxlunch_id: boxlunch_id)
|
190
206
|
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
def add_single_menu(single_menu_id)
|
212
|
+
|
213
|
+
cart_items.find_or_initialize_by(single_menu_id: single_menu_id)
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
```
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
-------------------------------------------------------------
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
カートアイテムのコントローラーのコード
|
230
|
+
|
231
|
+
```ruby
|
232
|
+
|
233
|
+
class CartItemsController < ApplicationController
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
def create
|
238
|
+
|
239
|
+
cart = current_cart
|
240
|
+
|
241
|
+
boxlunch = Boxlunch.find(params[:boxlunch_id])
|
242
|
+
|
243
|
+
@cart_item = current_cart.add_boxlunch(boxlunch.id)
|
244
|
+
|
245
|
+
#カレントカートは、アプリケーションコントローラーで定義しています。
|
246
|
+
|
247
|
+
if @cart_item.save
|
248
|
+
|
249
|
+
redirect_to cart_item_path(current_user)
|
250
|
+
|
251
|
+
else
|
252
|
+
|
253
|
+
render :new
|
254
|
+
|
191
255
|
end
|
192
256
|
|
257
|
+
end
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
private
|
262
|
+
|
263
|
+
def cart_item_params
|
264
|
+
|
265
|
+
params.require(:cart_item).permit(:boxlunch_quantity);
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
```
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
----------------------------------
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
アプリケーションコントローラーのコード
|
282
|
+
|
283
|
+
※current_cartを定義しています。
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
|
287
|
+
class ApplicationController < ActionController::Base
|
288
|
+
|
289
|
+
protect_from_forgery with: :exception
|
290
|
+
|
291
|
+
before_action :authenticate_user!
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
def after_sign_in_path_for(resource)
|
298
|
+
|
299
|
+
root_path # ログイン後に遷移するpathを設定
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
def after_sign_out_path_for(resource)
|
306
|
+
|
307
|
+
root_path # ログアウト後に遷移するpathを設定
|
308
|
+
|
309
|
+
end
|
310
|
+
|
193
311
|
|
194
312
|
|
313
|
+
private
|
314
|
+
|
195
|
-
|
315
|
+
def current_cart
|
196
|
-
|
316
|
+
|
197
|
-
|
317
|
+
@current_cart = Order.find_by(id: session[:order_id])
|
318
|
+
|
198
|
-
|
319
|
+
@current_cart = Order.create unless @current_cart
|
320
|
+
|
321
|
+
session[:order_id] = @current_cart.id
|
322
|
+
|
323
|
+
@current_cart
|
324
|
+
|
199
|
-
|
325
|
+
end
|
200
326
|
|
201
327
|
end
|
202
328
|
|
203
329
|
|
204
330
|
|
205
|
-
-------------------------------------------------------------
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
カートアイテムのコントローラーのコード
|
210
|
-
|
211
|
-
class CartItemsController < ApplicationController
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
def create
|
216
|
-
|
217
|
-
cart = current_cart
|
218
|
-
|
219
|
-
boxlunch = Boxlunch.find(params[:boxlunch_id])
|
220
|
-
|
221
|
-
@cart_item = current_cart.add_boxlunch(boxlunch.id)
|
222
|
-
|
223
|
-
※カレントカートは、アプリケーションコントローラーで定義しています。
|
224
|
-
|
225
|
-
if @cart_item.save
|
226
|
-
|
227
|
-
redirect_to cart_item_path(current_user)
|
228
|
-
|
229
|
-
else
|
230
|
-
|
231
|
-
render :new
|
232
|
-
|
233
|
-
|
331
|
+
```
|
234
|
-
|
235
|
-
end
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
private
|
240
|
-
|
241
|
-
def cart_item_params
|
242
|
-
|
243
|
-
params.require(:cart_item).permit(:boxlunch_quantity);
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
----------------------------------
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
アプリケーションコントローラーのコード
|
256
|
-
|
257
|
-
※current_cartを定義しています。
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
class ApplicationController < ActionController::Base
|
262
|
-
|
263
|
-
protect_from_forgery with: :exception
|
264
|
-
|
265
|
-
before_action :authenticate_user!
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
def after_sign_in_path_for(resource)
|
272
|
-
|
273
|
-
root_path # ログイン後に遷移するpathを設定
|
274
|
-
|
275
|
-
end
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
def after_sign_out_path_for(resource)
|
280
|
-
|
281
|
-
root_path # ログアウト後に遷移するpathを設定
|
282
|
-
|
283
|
-
end
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
private
|
288
|
-
|
289
|
-
def current_cart
|
290
|
-
|
291
|
-
@current_cart = Order.find_by(id: session[:order_id])
|
292
|
-
|
293
|
-
@current_cart = Order.create unless @current_cart
|
294
|
-
|
295
|
-
session[:order_id] = @current_cart.id
|
296
|
-
|
297
|
-
@current_cart
|
298
|
-
|
299
|
-
end
|
300
|
-
|
301
|
-
end
|
302
332
|
|
303
333
|
|
304
334
|
|
1
エラー内容の記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -84,6 +84,94 @@
|
|
84
84
|
|
85
85
|
|
86
86
|
|
87
|
+
エラー内容
|
88
|
+
|
89
|
+
chromeではエラーは表示されていないので
|
90
|
+
|
91
|
+
ターミナルでの記載になります。
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
Started POST "/cart_items?boxlunch_id=2" for ::1 at 2020-01-12 23:54:40 +0900
|
96
|
+
|
97
|
+
Processing by CartItemsController#create as HTML
|
98
|
+
|
99
|
+
Parameters: {"utf8"=>"✓", "authenticity_token"=>"NOg0zYOHm2sCIqeRmMCdz+pUbAfOax99Uy2cOnp4/CO4WwgRUsFUMg2RA0EYXS9VBHrBQ1HZAuQJsqaOGDtV+Q==", "cart_item"=>{"boxlunch_quantity"=>"2"}, "commit"=>"カートの中に入れる", "boxlunch_id"=>"2"}
|
100
|
+
|
101
|
+
Order Load (0.9ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" IS NULL LIMIT $1 [["LIMIT", 1]]
|
102
|
+
|
103
|
+
↳ app/controllers/application_controller.rb:16
|
104
|
+
|
105
|
+
(0.2ms) BEGIN
|
106
|
+
|
107
|
+
↳ app/controllers/application_controller.rb:17
|
108
|
+
|
109
|
+
(0.4ms) ROLLBACK
|
110
|
+
|
111
|
+
↳ app/controllers/application_controller.rb:17
|
112
|
+
|
113
|
+
Boxlunch Load (1.0ms) SELECT "boxlunches".* FROM "boxlunches" WHERE "boxlunches"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
|
114
|
+
|
115
|
+
↳ app/controllers/cart_items_controller.rb:5
|
116
|
+
|
117
|
+
Order Load (0.3ms) SELECT "orders".* FROM "orders" WHERE "orders"."id" IS NULL LIMIT $1 [["LIMIT", 1]]
|
118
|
+
|
119
|
+
↳ app/controllers/application_controller.rb:16
|
120
|
+
|
121
|
+
(0.2ms) BEGIN
|
122
|
+
|
123
|
+
↳ app/controllers/application_controller.rb:17
|
124
|
+
|
125
|
+
(0.1ms) ROLLBACK
|
126
|
+
|
127
|
+
↳ app/controllers/application_controller.rb:17
|
128
|
+
|
129
|
+
(0.2ms) BEGIN
|
130
|
+
|
131
|
+
↳ app/controllers/cart_items_controller.rb:7
|
132
|
+
|
133
|
+
CartItem Create (0.9ms) INSERT INTO "cart_items" ("boxlunch_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["boxlunch_id", 2], ["created_at", "2020-01-12 14:54:40.381901"], ["updated_at", "2020-01-12 14:54:40.381901"]]
|
134
|
+
|
135
|
+
↳ app/controllers/cart_items_controller.rb:7
|
136
|
+
|
137
|
+
(0.5ms) COMMIT
|
138
|
+
|
139
|
+
↳ app/controllers/cart_items_controller.rb:7
|
140
|
+
|
141
|
+
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
|
142
|
+
|
143
|
+
↳ app/controllers/cart_items_controller.rb:8
|
144
|
+
|
145
|
+
Redirected to http://localhost:3000/cart_items/1
|
146
|
+
|
147
|
+
Completed 302 Found in 33ms (ActiveRecord: 5.3ms)
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
Started GET "/cart_items/1" for ::1 at 2020-01-12 23:54:40 +0900
|
154
|
+
|
155
|
+
Processing by CartItemsController#show as HTML
|
156
|
+
|
157
|
+
Parameters: {"id"=>"1"}
|
158
|
+
|
159
|
+
Rendering cart_items/show.html.haml within layouts/application
|
160
|
+
|
161
|
+
Rendered cart_items/show.html.haml within layouts/application (1.6ms)
|
162
|
+
|
163
|
+
Completed 200 OK in 72ms (Views: 68.7ms | ActiveRecord: 0.0ms)
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
----------------------------------------------------------------------------
|
172
|
+
|
173
|
+
|
174
|
+
|
87
175
|
カートアイテムのモデルコード
|
88
176
|
|
89
177
|
class CartItem < ApplicationRecord
|