質問編集履歴
2
ビューの情報をより詳細に変更しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
現在、プログラミングを学習中の初学者です。
|
3
3
|
ポートフォリオ制作の一環でrailsでカート機能を備えたECサイトを作成しています。
|
4
4
|
|
5
|
+
ログイン状態でトップページに設定している、products#indexの「カートに入れる」ボタンを押すと、ユーザーidと紐づいたカートが生成され、cartテーブルとproductテーブルの中間テーブル的な役割を担うcart_itemテーブルに商品の個数と、それぞれのidを保存できるようにしたいです。
|
5
|
-
|
6
|
+
現在、「カートに入れる」ボタンを押すと、下記のエラーが吐かれてしまいます。
|
6
7
|
|
7
8
|
該当のフォームやルーティングに問題があるのでは無いかと思いますが、いろいろ調べてもどうしてよいか分かりません。
|
8
9
|
何日も解決できず困っています。アドバイスをいただけると幸いです。
|
@@ -41,8 +42,27 @@
|
|
41
42
|
#app/views/products/index.html.haml
|
42
43
|
#トップページと商品の購入ページを兼ねています。
|
43
44
|
|
45
|
+
%ul
|
46
|
+
- @products.each do |product|
|
47
|
+
%li.product
|
48
|
+
.pro-main
|
49
|
+
.pro-left
|
50
|
+
.pro-image
|
51
|
+
= image_tag product.image.url, height: "120"
|
52
|
+
.pro-name
|
53
|
+
= product.name
|
54
|
+
.pro-center
|
55
|
+
.pro-explain
|
56
|
+
= product.explain
|
57
|
+
.pro-right
|
58
|
+
.pro-price
|
59
|
+
= product.price
|
60
|
+
%span
|
61
|
+
円
|
62
|
+
#以下フォーム
|
44
|
-
=form_for([@product, @cart_item], url: add_item_products_path, method: :post) do |f|
|
63
|
+
=form_for([@product, @cart_item], url: add_item_products_path, method: :post) do |f|
|
45
64
|
=f.hidden_field :product_id, value: @product.id
|
65
|
+
/=f.hidden_field :cart_id, value: @cart.id
|
46
66
|
.pro-qua
|
47
67
|
= f.number_field :quantity, value: "0", style: "text-align:right", min: 0, class: "pro-quantity"
|
48
68
|
.pro-cart
|
1
うまく保存ができていなかったので、内容を変更しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,6 +39,8 @@
|
|
39
39
|
```
|
40
40
|
```
|
41
41
|
#app/views/products/index.html.haml
|
42
|
+
#トップページと商品の購入ページを兼ねています。
|
43
|
+
|
42
44
|
=form_for([@product, @cart_item], url: add_item_products_path, method: :post) do |f|
|
43
45
|
=f.hidden_field :product_id, value: @product.id
|
44
46
|
.pro-qua
|
@@ -48,6 +50,7 @@
|
|
48
50
|
```
|
49
51
|
```
|
50
52
|
#application_controller.rb
|
53
|
+
#current_cartを定義しています
|
51
54
|
helper_method :current_cart
|
52
55
|
|
53
56
|
def current_cart
|
@@ -134,20 +137,45 @@
|
|
134
137
|
```
|
135
138
|
```
|
136
139
|
#cart.rb
|
140
|
+
class Cart < ApplicationRecord
|
141
|
+
has_many :cart_items, dependent: :destroy
|
142
|
+
has_many :products, through: :cart_items
|
143
|
+
belongs_to :user
|
144
|
+
end
|
137
145
|
```
|
138
146
|
```
|
147
|
+
#cart_item.rb
|
148
|
+
class CartItem < ApplicationRecord
|
149
|
+
belongs_to :product
|
150
|
+
belongs_to :cart
|
151
|
+
end
|
139
152
|
```
|
140
153
|
```
|
154
|
+
#product.rb
|
155
|
+
class Product < ApplicationRecord
|
156
|
+
mount_uploader :image, ImageUploader
|
157
|
+
has_many :cart_items, dependent: :destroy
|
158
|
+
has_many :cart, through: :cart_items
|
159
|
+
end
|
141
160
|
```
|
142
161
|
```
|
162
|
+
#user.rb
|
163
|
+
class User < ApplicationRecord
|
164
|
+
has_one :cart, dependent: :destroy
|
165
|
+
end
|
143
166
|
```
|
144
167
|
|
145
168
|
|
146
169
|
|
147
170
|
### 試したこと
|
171
|
+
下記の記述等を参考にしましたがうまくいきませんでした。
|
148
172
|
|
173
|
+
[rails カート機能の実装(多対多)NoMethodError](https://teratail.com/questions/204294?link=qa_related_pc)
|
149
|
-
|
174
|
+
[RailsでEC系に良く出てくるカート機能を解説 / 実装してみた](https://qiita.com/Doppon/items/3947c0027d9ea555cc39)
|
175
|
+
[Rails5でカート機能を作るためのロジックを作ってみた](https://qiita.com/Coolucky/items/89ce3a0f25c9dfdb38c1)
|
150
176
|
|
177
|
+
ルーティングや、コントローラーの設定、フォームの使い方などに関する理解の浅さこそが最大の原因であると思いますが、どうにも進めなくなってしまい、お力添えをお願いしたいです。
|
178
|
+
|
151
179
|
### 補足情報(FW/ツールのバージョンなど)
|
152
180
|
|
153
|
-
|
181
|
+
Rails 5.0.7.2
|