質問編集履歴
5
内容に変更があったため
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
RailsでECサイトを作成中、商品をカートに追加しようとすると
|
1
|
+
RailsでECサイトを作成中、商品をカートに追加しようとするとNoMethodError in CartsController#add_itemのエラーが出る
|
body
CHANGED
@@ -136,7 +136,7 @@
|
|
136
136
|
if @product.save
|
137
137
|
redirect_to root_path
|
138
138
|
else
|
139
|
-
render
|
139
|
+
render @product.current_cart
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
4
変更があったため
title
CHANGED
File without changes
|
body
CHANGED
@@ -37,9 +37,9 @@
|
|
37
37
|
#app/views/products/index.html.haml
|
38
38
|
#トップページと商品の購入ページを兼ねています。
|
39
39
|
|
40
|
-
|
40
|
+
%ul
|
41
|
-
=form_for([@product, @cart_item], url: add_item_product_path, method: :post) do |f|
|
42
|
-
|
41
|
+
- @products.each do |product|
|
42
|
+
=form_for([@product, @cart_item], url: add_item_product_path(product), method: :post) do |f|
|
43
43
|
%li.product
|
44
44
|
.pro-main
|
45
45
|
.pro-left
|
@@ -67,11 +67,9 @@
|
|
67
67
|
helper_method :current_cart
|
68
68
|
|
69
69
|
def current_cart
|
70
|
-
@cart =Cart.
|
70
|
+
@cart = Cart.find_by(id: session[:cart_id]) || Cart.create
|
71
|
-
rescue ActiveRecord::RecordNotFound
|
72
|
-
cart = Cart.create
|
73
|
-
session[:cart_id] = cart.id
|
71
|
+
session[:cart_id] = @cart.id
|
74
|
-
cart
|
72
|
+
@cart
|
75
73
|
end
|
76
74
|
```
|
77
75
|
```
|
@@ -86,11 +84,11 @@
|
|
86
84
|
|
87
85
|
def add_item
|
88
86
|
@cart = current_cart
|
89
|
-
@product =Product.find(params[:
|
87
|
+
@product =Product.find(params[:id])
|
90
88
|
@cart_items = @cart.cart_items.build
|
91
89
|
|
92
90
|
if @cart_item.blank?
|
93
|
-
@cart_item = current_cart.cart_items.build(product_id: params[:
|
91
|
+
@cart_item = current_cart.cart_items.build(product_id: params[:id], cart_id: current_cart.id)
|
94
92
|
end
|
95
93
|
|
96
94
|
@cart_items.quantity += params[:quantity].to_i
|
@@ -126,7 +124,6 @@
|
|
126
124
|
|
127
125
|
def index
|
128
126
|
@products = Product.all
|
129
|
-
@product = CartItem.new
|
130
127
|
@cart_item = CartItem.new
|
131
128
|
end
|
132
129
|
|
@@ -180,7 +177,7 @@
|
|
180
177
|
end
|
181
178
|
```
|
182
179
|
```
|
183
|
-
|
180
|
+
Prefix Verb URI Pattern Controller#Action
|
184
181
|
new_user_session GET /users/sign_in(.:format) users/sessions#new
|
185
182
|
user_session POST /users/sign_in(.:format) users/sessions#create
|
186
183
|
destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy
|
@@ -201,6 +198,7 @@
|
|
201
198
|
root GET / products#index
|
202
199
|
product_carts POST /products/:product_id/carts(.:format) carts#create
|
203
200
|
new_product_cart GET /products/:product_id/carts/new(.:format) carts#new
|
201
|
+
product_cart GET /products/:product_id/carts/:id(.:format) carts#show
|
204
202
|
add_item_product POST /products/:id/add_item(.:format) carts#add_item
|
205
203
|
update_item_product POST /products/:id/update_item(.:format) carts#update_item
|
206
204
|
delete_item_product DELETE /products/:id/delete_item(.:format) carts#delete_item
|
@@ -214,9 +212,6 @@
|
|
214
212
|
user GET /users/:id(.:format) users#show
|
215
213
|
PATCH /users/:id(.:format) users#update
|
216
214
|
PUT /users/:id(.:format) users#update
|
217
|
-
carts POST /carts(.:format) carts#create
|
218
|
-
new_cart GET /carts/new(.:format) carts#new
|
219
|
-
cart GET /carts/:id(.:format) carts#show
|
220
215
|
```
|
221
216
|
|
222
217
|
|
3
変更があったため
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
|
22
22
|
resources :products, only: [:index, :new, :create, :edit, :update] do
|
23
23
|
resources :carts, only: [:create, :new]
|
24
|
-
|
24
|
+
member do
|
25
25
|
post '/add_item' => 'carts#add_item'
|
26
26
|
post '/update_item' => 'carts#update_item'
|
27
27
|
delete '/delete_item' => 'carts#delete_item'
|
@@ -37,27 +37,25 @@
|
|
37
37
|
#app/views/products/index.html.haml
|
38
38
|
#トップページと商品の購入ページを兼ねています。
|
39
39
|
|
40
|
-
%ul
|
40
|
+
%ul
|
41
|
+
=form_for([@product, @cart_item], url: add_item_product_path, method: :post) do |f|
|
41
|
-
|
42
|
+
- @products.each do |product|
|
42
|
-
|
43
|
+
%li.product
|
43
|
-
|
44
|
+
.pro-main
|
44
|
-
|
45
|
+
.pro-left
|
45
|
-
|
46
|
+
.pro-image
|
46
|
-
|
47
|
+
= image_tag product.image.url, height: "120"
|
47
|
-
|
48
|
+
.pro-name
|
48
|
-
|
49
|
+
= product.name
|
49
|
-
|
50
|
+
.pro-center
|
50
|
-
|
51
|
+
.pro-explain
|
51
|
-
|
52
|
+
= product.explain
|
52
|
-
|
53
|
+
.pro-right
|
53
|
-
|
54
|
+
.pro-price
|
54
|
-
|
55
|
+
= product.price
|
55
|
-
|
56
|
+
%span
|
56
|
-
|
57
|
+
円
|
57
|
-
#以下フォーム
|
58
|
-
=form_for([@product, @cart_item], url: add_item_products_path, method: :post) do |f|
|
59
|
-
=f.hidden_field :product_id, value:
|
58
|
+
=f.hidden_field :product_id, value: product.id
|
60
|
-
/=f.hidden_field :cart_id, value: @cart.id
|
61
59
|
.pro-qua
|
62
60
|
= f.number_field :quantity, value: "0", style: "text-align:right", min: 0, class: "pro-quantity"
|
63
61
|
.pro-cart
|
2
変更があったため
title
CHANGED
File without changes
|
body
CHANGED
@@ -203,9 +203,9 @@
|
|
203
203
|
root GET / products#index
|
204
204
|
product_carts POST /products/:product_id/carts(.:format) carts#create
|
205
205
|
new_product_cart GET /products/:product_id/carts/new(.:format) carts#new
|
206
|
-
|
206
|
+
add_item_product POST /products/:id/add_item(.:format) carts#add_item
|
207
|
-
|
207
|
+
update_item_product POST /products/:id/update_item(.:format) carts#update_item
|
208
|
-
|
208
|
+
delete_item_product DELETE /products/:id/delete_item(.:format) carts#delete_item
|
209
209
|
products GET /products(.:format) products#index
|
210
210
|
POST /products(.:format) products#create
|
211
211
|
new_product GET /products/new(.:format) products#new
|
1
ルーティングを載せるため
title
CHANGED
File without changes
|
body
CHANGED
@@ -181,6 +181,45 @@
|
|
181
181
|
has_one :cart, dependent: :destroy
|
182
182
|
end
|
183
183
|
```
|
184
|
+
```
|
185
|
+
Prefix Verb URI Pattern Controller#Action
|
186
|
+
new_user_session GET /users/sign_in(.:format) users/sessions#new
|
187
|
+
user_session POST /users/sign_in(.:format) users/sessions#create
|
188
|
+
destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy
|
189
|
+
new_user_password GET /users/password/new(.:format) devise/passwords#new
|
190
|
+
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
|
191
|
+
user_password PATCH /users/password(.:format) devise/passwords#update
|
192
|
+
PUT /users/password(.:format) devise/passwords#update
|
193
|
+
POST /users/password(.:format) devise/passwords#create
|
194
|
+
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
|
195
|
+
new_user_registration GET /users/sign_up(.:format) users/registrations#new
|
196
|
+
edit_user_registration GET /users/edit(.:format) users/registrations#edit
|
197
|
+
user_registration PATCH /users(.:format) users/registrations#update
|
198
|
+
PUT /users(.:format) users/registrations#update
|
199
|
+
DELETE /users(.:format) users/registrations#destroy
|
200
|
+
POST /users(.:format) users/registrations#create
|
201
|
+
sign_in GET /sign_in(.:format) users/sessions#new
|
202
|
+
sign_out GET /sign_out(.:format) users/sessions#destroy
|
203
|
+
root GET / products#index
|
204
|
+
product_carts POST /products/:product_id/carts(.:format) carts#create
|
205
|
+
new_product_cart GET /products/:product_id/carts/new(.:format) carts#new
|
206
|
+
add_item_products POST /products/add_item(.:format) carts#add_item
|
207
|
+
update_item_products POST /products/update_item(.:format) carts#update_item
|
208
|
+
delete_item_products DELETE /products/delete_item(.:format) carts#delete_item
|
209
|
+
products GET /products(.:format) products#index
|
210
|
+
POST /products(.:format) products#create
|
211
|
+
new_product GET /products/new(.:format) products#new
|
212
|
+
edit_product GET /products/:id/edit(.:format) products#edit
|
213
|
+
product PATCH /products/:id(.:format) products#update
|
214
|
+
PUT /products/:id(.:format) products#update
|
215
|
+
edit_user GET /users/:id/edit(.:format) users#edit
|
216
|
+
user GET /users/:id(.:format) users#show
|
217
|
+
PATCH /users/:id(.:format) users#update
|
218
|
+
PUT /users/:id(.:format) users#update
|
219
|
+
carts POST /carts(.:format) carts#create
|
220
|
+
new_cart GET /carts/new(.:format) carts#new
|
221
|
+
cart GET /carts/:id(.:format) carts#show
|
222
|
+
```
|
184
223
|
|
185
224
|
|
186
225
|
|