質問編集履歴

4

追記と変更点の記載

2022/09/27 13:28

投稿

pi-nattu
pi-nattu

スコア61

test CHANGED
File without changes
test CHANGED
@@ -176,4 +176,7 @@
176
176
  end
177
177
 
178
178
  ```
179
+ ```ruby/cart_items_html.erb変更しました
180
+ <%= link_to "カートを空にする", destroy_all_cart_items_path, method: :delete, "data-confirm" => "本当に空にしますか?" %><br>
181
+ ```
179
182
 

3

ルーティングファイルの記述を追記しました。

2022/09/27 13:16

投稿

pi-nattu
pi-nattu

スコア61

test CHANGED
File without changes
test CHANGED
@@ -135,4 +135,45 @@
135
135
  end
136
136
  end
137
137
  ```
138
+ 追記▼
139
+ ```ruby/routes.rb
140
+ Rails.application.routes.draw do
141
+
142
+ devise_for :admin, skip:[:registrations, :passwords] ,controllers: {
143
+ sessions: "admin/sessions"
144
+ }
145
+
146
+ get '/' => 'homes#top'
147
+ get '/about' => 'homes#about'
148
+ #customer用itemのパス
149
+ resources :items, only: [:index, :show]
150
+ namespace :admin do
151
+ get '/' => 'homes#top'
152
+
153
+ resources :items, only: [:new, :create, :index, :show, :edit, :update]
154
+ resources :genres, only: [:index, :edit, :create, :update]
155
+ end
156
+
157
+ #顧客用
158
+ devise_for :customers,skip: [:passwords], controllers: {
159
+ registrations: "public/registrations",
160
+ sessions: 'public/sessions'
161
+ }
138
162
 
163
+ #顧客情報関連
164
+ get '/customers/my_page' => 'customers#show'
165
+ get '/customers/infomation/edit' => 'customers#edit'
166
+ patch '/customers/infomation/up_date' => 'customers#update'
167
+ get '/customers/unsubscribe' => 'customers#unsubscribe'
168
+ patch '/customers/withdraw' => 'customers#withdraw'
169
+ #カート関連
170
+
171
+ delete '/cart_items/destroy_all' => 'cart_items#destroy_all', as: 'destroy_all_cart_items'
172
+ resources :cart_items, only: [:index, :update, :destroy, :create]
173
+ # 管理者用
174
+
175
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
176
+ end
177
+
178
+ ```
179
+

2

タグに不備があったので修正しました。

2022/09/26 20:38

投稿

pi-nattu
pi-nattu

スコア61

test CHANGED
File without changes
test CHANGED
File without changes

1

子度を追記しました

2022/09/26 15:06

投稿

pi-nattu
pi-nattu

スコア61

test CHANGED
File without changes
test CHANGED
@@ -74,8 +74,32 @@
74
74
  #合計金額の初期値は0円
75
75
  @total = 0
76
76
  end
77
+
78
+
79
+ def create
80
+ @cart_item = CartItem.find_by(item_id: params[:cart_item][:item_id])
81
+ if @cart_item
82
+ #カートにitemが存在したらamountに新しいCart_itemのamountを足す
83
+ @cart_item.amount += CartItem.new(cart_item_params).amount
84
+ else
85
+ #カートにitemが無ければ新しく作成する
86
+ @cart_item = CartItem.new(cart_item_params)
87
+ end
88
+ #ログインcustomerのみ更新できるようにするため分岐の外に記述する
89
+ @cart_item.customer_id = current_customer.id
90
+ @cart_item.save!
91
+ redirect_to cart_items_path
92
+ end
77
93
 
94
+
95
+ def update
96
+ @cart_item = current_customer
97
+ @item = Item.all
98
+ @cart_item.update(cart_item_params)
99
+ redirect_to cart_items_path
100
+ end
101
+
78
- def destroy
102
+ def destroy
79
103
  @cart_item = CartItem.find(params[:id])
80
104
  @cart_item.destroy!
81
105
  redirect_to cart_items_path
@@ -86,5 +110,29 @@
86
110
  current_customer.cart_item.destroy_all
87
111
  redirect_to cart_items_path, notice: 'カートが空になりました。'
88
112
  end
113
+
114
+ private
115
+ def cart_item_params
116
+ params.require(:cart_item).permit(:item_id, :amount)
117
+ end
118
+ end
119
+ ```
120
+ ```ruby/items_controller.rb
121
+ class ItemsController < ApplicationController
122
+ def index
123
+ @items = Item.all
124
+ @genres = Genre.all
125
+ end
126
+
127
+ def show
128
+ @item = Item.find(params[:id])
129
+ @cart_item = CartItem.new
130
+ end
131
+
132
+ def item_params
133
+ params.require(:item).permit(
134
+ :name, :item_image, :introduction, :item_price, :genre_id)
135
+ end
136
+ end
89
137
  ```
90
138