teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

文法の修正

2020/05/17 03:48

投稿

kabigon
kabigon

スコア3

title CHANGED
@@ -1,1 +1,1 @@
1
- テーブルをリセットする機能を追加したい
1
+ Routing Error
body CHANGED
File without changes

1

書式の改善

2020/05/17 03:48

投稿

kabigon
kabigon

スコア3

title CHANGED
File without changes
body CHANGED
@@ -2,27 +2,106 @@
2
2
  なかなか削除できずにいます。
3
3
  destroyアクションに記載してあるのは"消"の方のボタンで
4
4
  今回実装できずにいるのは"RESET"のボタンです。
5
- 削除ボタンが2つあるとdestroyアクションに両方記載できるのでしょうか
6
5
 
6
+ **Routing Error**
7
+ No route matches [POST] "/orders/destroy_all"
7
8
 
8
9
 
10
+
11
+
9
12
  ##controller
10
13
  ```
14
+ class OrdersController < ApplicationController
15
+
16
+ def index
17
+ @user_food = UserFood.new
18
+
19
+ @user_foods = UserFood.all
20
+ price = UserFood.sum(:price)
21
+ quantity = UserFood.sum(:quantity)
22
+ @total = price
23
+ end
24
+
25
+ def new
26
+ end
27
+
28
+ def create
29
+ user_food = UserFood.find_by(food: params[:food])
30
+ if user_food.nil?
31
+ user_food = UserFood.new(user_food_params)
32
+
33
+ if user_food.save
34
+ redirect_to root_path
35
+ else
36
+ render order: :index
37
+ end
38
+
39
+ else
40
+ user_food.increment(quantity:1);
41
+ end
42
+ end
43
+
11
- def destroy
44
+ def destroy
12
45
  order = UserFood.find(params[:id])
13
46
  order.destroy
14
47
  redirect_to root_path
15
48
  end
49
+
50
+ def destroy_all
51
+ UserFood.destroy_all
52
+ redirect_to root_path
53
+ end
54
+
55
+ private
56
+ def user_food_params
57
+ params.require(:user_food).permit(:food, :price, :quantity)
58
+ end
59
+
60
+ end
61
+
16
62
  ```
17
63
 
18
64
  ##index.html.haml
19
65
  ```
66
+ .right-bar
67
+ .order-list
68
+ - @user_foods.each do |user_food|
69
+ .order-box
70
+ .order-box__deta
20
- .order-box__deta--name
71
+ .order-box__deta--name
21
72
  = user_food.food
22
73
  .order-box__deta--number
23
74
  = user_food.quantity
24
75
  .order-box__deta--delete
25
76
  = link_to "消", order_path(user_food), method: :delete
26
77
  .reset-box
78
+ = link_to "RESET", destroy_all_orders_path, method: :derete
79
+ .price-box
80
+ .price-box__deta
81
+ .price-box__deta--yen
82
+ ¥
83
+ .price-box__deta--price
84
+ = @total
85
+ ```
86
+ ##Routes.rb
87
+ ```
88
+ Rails.application.routes.draw do
27
- = link_to "RESET",:
89
+ root "orders#index"
90
+ resources :orders, only: [:index, :new, :create, :destroy,]do
91
+ collection do
92
+ delete 'destroy_all'
93
+ end
94
+ end
95
+ end
96
+
97
+ ```
98
+ ##rails routes
99
+ ```
100
+ Prefix Verb URI Pattern Controller#Action
101
+ root GET / orders#index
102
+ destroy_all_orders DELETE /orders/destroy_all(.:format) orders#destroy_all
103
+ orders GET /orders(.:format) orders#index
104
+ POST /orders(.:format) orders#create
105
+ new_order GET /orders/new(.:format) orders#new
106
+ order DELETE /orders/:id(.:format) orders#destroy
28
107
  ```