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

質問編集履歴

2

修正

2020/09/13 01:29

投稿

tomato185
tomato185

スコア29

title CHANGED
File without changes
body CHANGED
@@ -15,7 +15,7 @@
15
15
  ログを見ると、`"product_id"=>["6", "1"], "quantity"=>["1", "2"],`というように、配列で送られているところまでは実装できましたが、
16
16
  テーブルにsaveする時(もしくはその一個手前:今回の場合、`address_params`アクション)にSQL文が発行されないのではないかと考えております。
17
17
 
18
- 配列で送られてきたデータをテーブルに送信する方法をどなたか教えていただけませんでしょうか。
18
+ 配列で送られてきたデータをテーブルに1つずつ(2つの商品があれば2レコード)送信する方法をどなたか教えていただけませんでしょうか。
19
19
 
20
20
  ### 該当のソースコード
21
21
 

1

修正

2020/09/13 01:29

投稿

tomato185
tomato185

スコア29

title CHANGED
File without changes
body CHANGED
@@ -78,9 +78,63 @@
78
78
 
79
79
  private
80
80
 
81
- def address_params  ★ここがおかしいのではないか????
81
+ def address_params
82
82
  params.require(:address).permit(:user_id, :last_name, :first_name, :furi_last_name, :furi_first_name, :postal_code, :prefecture, :address, :store, :how_to_pay, orders_attributes: [
83
83
  product_id:[], quantity:[]
84
84
  ])
85
85
  end
86
+ ```
87
+
88
+ ```
89
+ (models/address.rb)
90
+ class Address < ApplicationRecord
91
+ has_many :orders
92
+ accepts_nested_attributes_for :orders
93
+ end
94
+
95
+ addressesテーブル(カラム)
96
+ id
97
+ user_id
98
+ last_name
99
+ first_name
100
+ furi_last_name
101
+ furi_first_name
102
+ postal_code
103
+ prefecture
104
+ address
105
+ store
106
+ how_to_pay
107
+
108
+
109
+ ```
110
+
111
+ ```
112
+ (models/order.rb)
113
+ class Order < ApplicationRecord
114
+ belongs_to :address
115
+ belongs_to :product
116
+ end
117
+
118
+ ordersテーブル(カラム)
119
+ id
120
+ product_id
121
+ quantity
122
+ address_id
123
+ ```
124
+
125
+ ```
126
+ (models/product.rb)※一部記載
127
+ class Product < ApplicationRecord
128
+
129
+ has_many :orders
130
+
131
+ end
132
+
133
+ productsテーブル(カラム)
134
+ id
135
+ name
136
+ price
137
+ count
138
+ comment
139
+ category
86
140
  ```