質問編集履歴

4

formオブジェクトの記述を変えてみました。

2020/12/10 09:42

投稿

murohi-08
murohi-08

スコア12

test CHANGED
File without changes
test CHANGED
@@ -376,7 +376,7 @@
376
376
 
377
377
  ```ここに言語を入力
378
378
 
379
- app/models/user_item.rb
379
+ app/models/user_purchase_history.rb
380
380
 
381
381
 
382
382
 

3

formオブジェクトの記述を変えてみました。

2020/12/10 09:42

投稿

murohi-08
murohi-08

スコア12

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,8 @@
14
14
 
15
15
  ```ここに言語を入力
16
16
 
17
+
18
+
17
19
  app/controllers/purchase_histories_controller.rb
18
20
 
19
21
 
@@ -66,9 +68,9 @@
66
68
 
67
69
  end
68
70
 
69
- end```
71
+ end
72
+
70
-
73
+ ```
71
-
72
74
 
73
75
  ```ここに言語を入力
74
76
 

2

formオブジェクトの記述を変えてみました。

2020/12/10 07:53

投稿

murohi-08
murohi-08

スコア12

test CHANGED
File without changes
test CHANGED
@@ -338,6 +338,10 @@
338
338
 
339
339
  ```
340
340
 
341
+
342
+
343
+
344
+
341
345
  ```ここに言語を入力
342
346
 
343
347
  config/routes.rb

1

formオブジェクトの記述を変えてみました。

2020/12/10 07:51

投稿

murohi-08
murohi-08

スコア12

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  def index
24
24
 
25
- @user_items = UserItem.all #この部分
25
+ @user_purchase_histories = UserPurchaseHistory.all
26
26
 
27
27
  end
28
28
 
@@ -30,7 +30,7 @@
30
30
 
31
31
  def new
32
32
 
33
- @user_item = UserItem.new
33
+ @user_purchase_history = UserPurchaseHistory.new
34
34
 
35
35
  end
36
36
 
@@ -38,11 +38,11 @@
38
38
 
39
39
  def create
40
40
 
41
- @user_item = UserItem.new(purchase_history_params)
41
+ @user_purchase_history = UserPurchaseHistory.new(purchase_history_params)
42
-
42
+
43
- if @user_item.valid?
43
+ if @user_purchase_history.valid?
44
-
44
+
45
- @user_item.save
45
+ @user_purchase_history.save
46
46
 
47
47
  redirect_to action: :index
48
48
 
@@ -62,7 +62,297 @@
62
62
 
63
63
  def purchase_history_params
64
64
 
65
- params.require(:user_item).permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :name, :introduction, :category, :item_condition, :postage_id, :prefecture_id, :prepare_id, :price)
65
+ params.require(:user_purchase_history).permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana)
66
+
67
+ end
68
+
69
+ end```
70
+
71
+
72
+
73
+ ```ここに言語を入力
74
+
75
+ views/parchase_history/index.html.erb
76
+
77
+
78
+
79
+ <%= render "shared/second-header"%>
80
+
81
+
82
+
83
+ <div class='transaction-contents'>
84
+
85
+ <div class='transaction-main'>
86
+
87
+ <h1 class='transaction-title-text'>
88
+
89
+ 購入内容の確認
90
+
91
+ </h1>
92
+
93
+ <%# 購入内容の表示 %>
94
+
95
+ <div class='buy-item-info'>
96
+
97
+ <%= image_tag "item-sample.png", class: 'buy-item-img' %>
98
+
99
+ <div class='buy-item-right-content'>
100
+
101
+ <h2 class='buy-item-text'>
102
+
103
+ <%= "商品名" %>
104
+
105
+ </h2>
106
+
107
+ <div class='buy-item-price'>
108
+
109
+ <p class='item-price-text'>¥<%= 999,999,999 %></p>
110
+
111
+ <p class='item-price-sub-text'><%= '配送料負担' %></p>
112
+
113
+ </div>
114
+
115
+ </div>
116
+
117
+ </div>
118
+
119
+ <%# /購入内容の表示 %>
120
+
121
+
122
+
123
+ <%# 支払額の表示 %>
124
+
125
+ <div class='item-payment'>
126
+
127
+ <h1 class='item-payment-title'>
128
+
129
+ 支払金額
130
+
131
+ </h1>
132
+
133
+ <p class='item-payment-price'>
134
+
135
+ ¥<%= "販売価格" %>
136
+
137
+ </p>
138
+
139
+ </div>
140
+
141
+ <%# /支払額の表示 %>
142
+
143
+
144
+
145
+ <%= form_with model: @user_purchase_history, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
146
+
147
+ <%# カード情報の入力 %>
148
+
149
+ <div class='credit-card-form'>
150
+
151
+ <h1 class='info-input-haedline'>
152
+
153
+ クレジットカード情報入力
154
+
155
+ </h1>
156
+
157
+ <div class="form-group">
158
+
159
+ <div class='form-text-wrap'>
160
+
161
+ <label class="form-text">カード情報</label>
162
+
163
+ <span class="indispensable">必須</span>
164
+
165
+ </div>
166
+
167
+ <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16" %>
168
+
169
+ <div class='available-card'>
170
+
171
+ <%= image_tag 'card-visa.gif', class: 'card-logo'%>
172
+
173
+ <%= image_tag 'card-mastercard.gif', class: 'card-logo'%>
174
+
175
+ <%= image_tag 'card-jcb.gif', class: 'card-logo'%>
176
+
177
+ <%= image_tag 'card-amex.gif', class: 'card-logo'%>
178
+
179
+ </div>
180
+
181
+ </div>
182
+
183
+ <div class="form-group">
184
+
185
+ <div class='form-text-wrap'>
186
+
187
+ <label class="form-text">有効期限</label>
188
+
189
+ <span class="indispensable">必須</span>
190
+
191
+ </div>
192
+
193
+ <div class='input-expiration-date-wrap'>
194
+
195
+ <%= f.text_area :exp_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3" %>
196
+
197
+ <p>月</p>
198
+
199
+ <%= f.text_area :exp_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23" %>
200
+
201
+ <p>年</p>
202
+
203
+ </div>
204
+
205
+ </div>
206
+
207
+ <div class="form-group">
208
+
209
+ <div class='form-text-wrap'>
210
+
211
+ <label class="form-text">セキュリティコード</label>
212
+
213
+ <span class="indispensable">必須</span>
214
+
215
+ </div>
216
+
217
+ <%= f.text_field :cvc,class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4" %>
218
+
219
+ </div>
220
+
221
+ </div>
222
+
223
+ <%# /カード情報の入力 %>
224
+
225
+
226
+
227
+ <%# 配送先の入力 %>
228
+
229
+ <div class='shipping-address-form'>
230
+
231
+ <h1 class='info-input-haedline'>
232
+
233
+ 配送先入力
234
+
235
+ </h1>
236
+
237
+ <div class="form-group">
238
+
239
+ <div class='form-text-wrap'>
240
+
241
+ <label class="form-text">郵便番号</label>
242
+
243
+ <span class="indispensable">必須</span>
244
+
245
+ </div>
246
+
247
+ <%= f.text_field :post_code, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
248
+
249
+ </div>
250
+
251
+ <div class="form-group">
252
+
253
+ <div class='form-text-wrap'>
254
+
255
+ <label class="form-text">都道府県</label>
256
+
257
+ <span class="indispensable">必須</span>
258
+
259
+ </div>
260
+
261
+ <%= f.collection_select(:prefecture_id, Prefecture.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
262
+
263
+ </div>
264
+
265
+ <div class="form-group">
266
+
267
+ <div class='form-text-wrap'>
268
+
269
+ <label class="form-text">市区町村</label>
270
+
271
+ <span class="indispensable">必須</span>
272
+
273
+ </div>
274
+
275
+ <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
276
+
277
+ </div>
278
+
279
+ <div class="form-group">
280
+
281
+ <div class='form-text-wrap'>
282
+
283
+ <label class="form-text">番地</label>
284
+
285
+ <span class="indispensable">必須</span>
286
+
287
+ </div>
288
+
289
+ <%= f.text_field :house_number, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
290
+
291
+ </div>
292
+
293
+ <div class="form-group">
294
+
295
+ <div class='form-text-wrap'>
296
+
297
+ <label class="form-text">建物名</label>
298
+
299
+ <span class="form-any">任意</span>
300
+
301
+ </div>
302
+
303
+ <%= f.text_field :building_name, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
304
+
305
+ </div>
306
+
307
+ <div class="form-group">
308
+
309
+ <div class='form-text-wrap'>
310
+
311
+ <label class="form-text">電話番号</label>
312
+
313
+ <span class="indispensable">必須</span>
314
+
315
+ </div>
316
+
317
+ <%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
318
+
319
+ </div>
320
+
321
+ </div>
322
+
323
+ <%# /配送先の入力 %>
324
+
325
+ <div class='buy-btn'>
326
+
327
+ <%= f.submit "購入" ,class:"buy-red-btn" %>
328
+
329
+ </div>
330
+
331
+ <% end %>
332
+
333
+ </div>
334
+
335
+ </div>
336
+
337
+ <%= render "shared/second-footer"%>
338
+
339
+ ```
340
+
341
+ ```ここに言語を入力
342
+
343
+ config/routes.rb
344
+
345
+
346
+
347
+ Rails.application.routes.draw do
348
+
349
+ devise_for :users
350
+
351
+ root "items#index"
352
+
353
+ resources :items do
354
+
355
+ resources :purchase_histories, only: [:index, :new, :create]
66
356
 
67
357
  end
68
358
 
@@ -72,376 +362,60 @@
72
362
 
73
363
 
74
364
 
365
+
366
+
367
+ 以下formオブジェクトのモデルファイルになります。
368
+
369
+
370
+
75
371
  ```ここに言語を入力
76
372
 
77
- views/parchase_history/index.html.erb
78
-
79
-
80
-
81
- <%= render "shared/second-header"%>
82
-
83
-
84
-
85
- <div class='transaction-contents'>
86
-
87
- <div class='transaction-main'>
88
-
89
- <h1 class='transaction-title-text'>
90
-
91
- 購入内容の確認
92
-
93
- </h1>
94
-
95
- <%# 購入内容の表示 %>
96
-
97
- <div class='buy-item-info'>
98
-
99
- <%= image_tag "item-sample.png", class: 'buy-item-img' %>
100
-
101
- <div class='buy-item-right-content'>
102
-
103
- <h2 class='buy-item-text'>
104
-
105
- <%= "商品名" %>
106
-
107
- </h2>
108
-
109
- <div class='buy-item-price'>
110
-
111
- <p class='item-price-text'>¥<%= 999,999,999 %></p>
112
-
113
- <p class='item-price-sub-text'><%= '配送料負担' %></p>
114
-
115
- </div>
116
-
117
- </div>
118
-
119
- </div>
120
-
121
- <%# /購入内容の表示 %>
122
-
123
-
124
-
125
- <%# 支払額の表示 %>
126
-
127
- <div class='item-payment'>
128
-
129
- <h1 class='item-payment-title'>
130
-
131
- 支払金額
132
-
133
- </h1>
134
-
135
- <p class='item-payment-price'>
136
-
137
- ¥<%= "販売価格" %>
138
-
139
- </p>
140
-
141
- </div>
142
-
143
- <%# /支払額の表示 %>
144
-
145
-
146
-
147
- <%= form_with model: @user_items, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
148
-
149
- <%# カード情報の入力 %>
150
-
151
- <div class='credit-card-form'>
152
-
153
- <h1 class='info-input-haedline'>
154
-
155
- クレジットカード情報入力
156
-
157
- </h1>
158
-
159
- <div class="form-group">
160
-
161
- <div class='form-text-wrap'>
162
-
163
- <label class="form-text">カード情報</label>
164
-
165
- <span class="indispensable">必須</span>
166
-
167
- </div>
168
-
169
- <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16" %>
170
-
171
- <div class='available-card'>
172
-
173
- <%= image_tag 'card-visa.gif', class: 'card-logo'%>
174
-
175
- <%= image_tag 'card-mastercard.gif', class: 'card-logo'%>
176
-
177
- <%= image_tag 'card-jcb.gif', class: 'card-logo'%>
178
-
179
- <%= image_tag 'card-amex.gif', class: 'card-logo'%>
180
-
181
- </div>
182
-
183
- </div>
184
-
185
- <div class="form-group">
186
-
187
- <div class='form-text-wrap'>
188
-
189
- <label class="form-text">有効期限</label>
190
-
191
- <span class="indispensable">必須</span>
192
-
193
- </div>
194
-
195
- <div class='input-expiration-date-wrap'>
196
-
197
- <%= f.text_area :exp_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3" %>
198
-
199
- <p>月</p>
200
-
201
- <%= f.text_area :exp_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23" %>
202
-
203
- <p>年</p>
204
-
205
- </div>
206
-
207
- </div>
208
-
209
- <div class="form-group">
210
-
211
- <div class='form-text-wrap'>
212
-
213
- <label class="form-text">セキュリティコード</label>
214
-
215
- <span class="indispensable">必須</span>
216
-
217
- </div>
218
-
219
- <%= f.text_field :cvc,class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4" %>
220
-
221
- </div>
222
-
223
- </div>
224
-
225
- <%# /カード情報の入力 %>
226
-
227
-
228
-
229
- <%# 配送先の入力 %>
230
-
231
- <div class='shipping-address-form'>
232
-
233
- <h1 class='info-input-haedline'>
234
-
235
- 配送先入力
236
-
237
- </h1>
238
-
239
- <div class="form-group">
240
-
241
- <div class='form-text-wrap'>
242
-
243
- <label class="form-text">郵便番号</label>
244
-
245
- <span class="indispensable">必須</span>
246
-
247
- </div>
248
-
249
- <%= f.text_field :post_code, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
250
-
251
- </div>
252
-
253
- <div class="form-group">
254
-
255
- <div class='form-text-wrap'>
256
-
257
- <label class="form-text">都道府県</label>
258
-
259
- <span class="indispensable">必須</span>
260
-
261
- </div>
262
-
263
- <%= f.collection_select(:prefecture_id, Prefecture.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
264
-
265
- </div>
266
-
267
- <div class="form-group">
268
-
269
- <div class='form-text-wrap'>
270
-
271
- <label class="form-text">市区町村</label>
272
-
273
- <span class="indispensable">必須</span>
274
-
275
- </div>
276
-
277
- <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
278
-
279
- </div>
280
-
281
- <div class="form-group">
282
-
283
- <div class='form-text-wrap'>
284
-
285
- <label class="form-text">番地</label>
286
-
287
- <span class="indispensable">必須</span>
288
-
289
- </div>
290
-
291
- <%= f.text_field :house_number, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
292
-
293
- </div>
294
-
295
- <div class="form-group">
296
-
297
- <div class='form-text-wrap'>
298
-
299
- <label class="form-text">建物名</label>
300
-
301
- <span class="form-any">任意</span>
302
-
303
- </div>
304
-
305
- <%= f.text_field :building_name, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
306
-
307
- </div>
308
-
309
- <div class="form-group">
310
-
311
- <div class='form-text-wrap'>
312
-
313
- <label class="form-text">電話番号</label>
314
-
315
- <span class="indispensable">必須</span>
316
-
317
- </div>
318
-
319
- <%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
320
-
321
- </div>
322
-
323
- </div>
324
-
325
- <%# /配送先の入力 %>
326
-
327
- <div class='buy-btn'>
328
-
329
- <%= f.submit "購入" ,class:"buy-red-btn" %>
330
-
331
- </div>
332
-
333
- <% end %>
334
-
335
- </div>
336
-
337
- </div>
338
-
339
- <%= render "shared/second-footer"%>
373
+ app/models/user_item.rb
374
+
375
+
376
+
377
+ class UserPurchaseHistory
378
+
379
+ include ActiveModel::Model
380
+
381
+ attr_accessor :nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :birth_day
382
+
383
+
384
+
385
+ with_options presence: true do
386
+
387
+ validates :nickname
388
+
389
+ validates :email
390
+
391
+ validates :password, format: { with: /\A[a-zA-Z0-9]+\z/, message: "Include both letters and numbers."}
392
+
393
+ validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."}
394
+
395
+ validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."}
396
+
397
+ validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."}
398
+
399
+ validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."}
400
+
401
+ validates :birth_day
402
+
403
+ end
404
+
405
+
406
+
407
+ def save
408
+
409
+ user = User.create(nickname: nickname, email: email, password: password, first_name: first_name, family_name: family_name, first_name_kana: first_name_kana, family_name_kana: family_name_kana, birth_day: birth_day)
410
+
411
+ SendingDestination.create(post_code: post_code, prefecture_id: prefecture.id, city: city, house_number: house_number, building_name: building_name, phone_number: phone_number, purchase_history_id: purchase_history.id)
412
+
413
+ Purchase_history.create(user_id: user.id, item_id: item.id)
414
+
415
+ end
416
+
417
+
418
+
419
+ end
340
420
 
341
421
  ```
342
-
343
- 以下formオブジェクトのモデルファイルになります。
344
-
345
-
346
-
347
- ```ここに言語を入力
348
-
349
- app/models/user_item.rb
350
-
351
-
352
-
353
- class UserItem
354
-
355
- include ActiveModel::Model
356
-
357
- attr_accessor :nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :name, :introduction, :category, :item_condition, :postage_id, :prefecture_id, :prepare_id, :price
358
-
359
-
360
-
361
- with_options presence: true do
362
-
363
- validates :nickname
364
-
365
- validates :email
366
-
367
- validates :password, format: { with: /\A[a-zA-Z0-9]+\z/, message: "Include both letters and numbers."}
368
-
369
- validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."}
370
-
371
- validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."}
372
-
373
- validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."}
374
-
375
- validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."}
376
-
377
- validates :birth_day
378
-
379
- validates :image
380
-
381
- validates :name
382
-
383
- validates :introduction
384
-
385
- validates :price
386
-
387
- end
388
-
389
- validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999, message: "Out of setting range"}
390
-
391
- validates :price, numericality: { only_integer: true, message: "Half-width number." }
392
-
393
-
394
-
395
- with_options numericality: { other_than: 0, message: "Select" } do
396
-
397
- validates :item_condition_id
398
-
399
- validates :postage_id
400
-
401
- validates :prefecture_id
402
-
403
- validates :prepare_id
404
-
405
- validates :category_id
406
-
407
- end
408
-
409
-
410
-
411
- def save
412
-
413
- user = User.create(nickname: nickname, email: email, password: password, first_name: first_name, family_name: family_name, first_name_kana: first_name_kana, family_name_kana: family_name_kana, birth_day: birth_day)
414
-
415
- Item.create(name: name, introduction: introduction, category_id: category.id, item_condition_id: item_condition.id, postage_id: postage.id, prefecture_id: prefecture.id, prepare_id: prepare.id, price: price, user_id: user.id)
416
-
417
- end
418
-
419
-
420
-
421
- end
422
-
423
- ```
424
-
425
-
426
-
427
- ```ここに言語を入力
428
-
429
- config/routes.rb
430
-
431
-
432
-
433
- Rails.application.routes.draw do
434
-
435
- devise_for :users
436
-
437
- root "items#index"
438
-
439
- resources :items do
440
-
441
- resources :purchase_histories, only: [:index, :new, :create]
442
-
443
- end
444
-
445
- end
446
-
447
- ```