質問編集履歴
1
初心者故冗長な内容ですみません。質問内容の書き直し及び必要のなさそうな部分は省きました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
+
現在、フリマアプリの購入機能の部分を作成中です。
|
4
|
+
|
3
|
-
入力エラーを起こしたときに
|
5
|
+
購入の際に送付先の住所を入力するのですがバリデーションによる入力エラーを起こしたときに住所の部分をそのままにリダイレクトしたいのです。
|
4
6
|
|
5
7
|
### 発生している問題・エラーメッセージ
|
6
8
|
|
7
|
-
|
9
|
+
バリデーションおよびリダイレクトはしっかりと機能していますが情報は保持されず住所の部分は空欄になった状態に戻ってしまいいます。
|
8
10
|
|
9
11
|
|
10
12
|
|
@@ -36,429 +38,297 @@
|
|
36
38
|
|
37
39
|
購入内容の確認
|
38
40
|
|
41
|
+
|
42
|
+
|
43
|
+
【長いので一部省略】
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
<%# 配送先の入力 %>
|
48
|
+
|
49
|
+
<div class='shipping-address-form'>
|
50
|
+
|
51
|
+
<h1 class='info-input-haedline'>
|
52
|
+
|
53
|
+
配送先入力
|
54
|
+
|
39
|
-
</h1>
|
55
|
+
</h1>
|
40
|
-
|
41
|
-
|
56
|
+
|
42
|
-
|
43
|
-
<div class=
|
57
|
+
<div class="form-group">
|
44
|
-
|
45
|
-
|
58
|
+
|
46
|
-
|
47
|
-
<div class='buy-item-right-content'>
|
48
|
-
|
49
|
-
<h2 class='buy-item-text'>
|
50
|
-
|
51
|
-
<%= @item.item_name %>
|
52
|
-
|
53
|
-
</h2>
|
54
|
-
|
55
|
-
<div class='
|
59
|
+
<div class='form-text-wrap'>
|
60
|
+
|
56
|
-
|
61
|
+
<label class="form-text">郵便番号</label>
|
62
|
+
|
57
|
-
<p class=
|
63
|
+
<span class="indispensable">必須</span>
|
58
|
-
|
59
|
-
|
64
|
+
|
60
|
-
|
61
|
-
</div>
|
65
|
+
</div>
|
66
|
+
|
67
|
+
<%= f.text_field :postal_code, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
|
68
|
+
|
69
|
+
</div>
|
70
|
+
|
71
|
+
<div class="form-group">
|
72
|
+
|
73
|
+
<div class='form-text-wrap'>
|
74
|
+
|
75
|
+
<label class="form-text">都道府県</label>
|
76
|
+
|
77
|
+
<span class="indispensable">必須</span>
|
78
|
+
|
79
|
+
</div>
|
80
|
+
|
81
|
+
<%= f.collection_select(:prefectures_id, Prefectures.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
|
82
|
+
|
83
|
+
</div>
|
84
|
+
|
85
|
+
<div class="form-group">
|
86
|
+
|
87
|
+
<div class='form-text-wrap'>
|
88
|
+
|
89
|
+
<label class="form-text">市区町村</label>
|
90
|
+
|
91
|
+
<span class="indispensable">必須</span>
|
92
|
+
|
93
|
+
</div>
|
94
|
+
|
95
|
+
<%= f.text_field :municipality, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
|
96
|
+
|
97
|
+
</div>
|
98
|
+
|
99
|
+
<div class="form-group">
|
100
|
+
|
101
|
+
<div class='form-text-wrap'>
|
102
|
+
|
103
|
+
<label class="form-text">番地</label>
|
104
|
+
|
105
|
+
<span class="indispensable">必須</span>
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
<%= f.text_field :address, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
|
110
|
+
|
111
|
+
</div>
|
112
|
+
|
113
|
+
<div class="form-group">
|
114
|
+
|
115
|
+
<div class='form-text-wrap'>
|
116
|
+
|
117
|
+
<label class="form-text">建物名</label>
|
118
|
+
|
119
|
+
<span class="form-any">任意</span>
|
120
|
+
|
121
|
+
</div>
|
122
|
+
|
123
|
+
<%= f.text_field :building, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
|
124
|
+
|
125
|
+
</div>
|
126
|
+
|
127
|
+
<div class="form-group">
|
128
|
+
|
129
|
+
<div class='form-text-wrap'>
|
130
|
+
|
131
|
+
<label class="form-text">電話番号</label>
|
132
|
+
|
133
|
+
<span class="indispensable">必須</span>
|
134
|
+
|
135
|
+
</div>
|
136
|
+
|
137
|
+
<%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
|
62
138
|
|
63
139
|
</div>
|
64
140
|
|
65
141
|
</div>
|
66
142
|
|
67
|
-
<%# /
|
143
|
+
<%# /配送先の入力 %>
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
144
|
+
|
72
|
-
|
73
|
-
<div class='
|
145
|
+
<div class='buy-btn'>
|
74
|
-
|
146
|
+
|
75
|
-
<
|
147
|
+
<%= f.submit "購入" ,class:"buy-red-btn", id:"button" %>
|
76
|
-
|
77
|
-
支払金額
|
78
|
-
|
79
|
-
</h1>
|
80
|
-
|
81
|
-
<p class='item-payment-price'>
|
82
|
-
|
83
|
-
¥<%= @item.price %>
|
84
|
-
|
85
|
-
</p>
|
86
148
|
|
87
149
|
</div>
|
88
150
|
|
89
|
-
<%
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
151
|
+
<% end %>
|
152
|
+
|
153
|
+
</div>
|
154
|
+
|
155
|
+
</div>
|
156
|
+
|
157
|
+
<%= render "shared/second-footer"%>
|
158
|
+
|
159
|
+
```
|
160
|
+
|
161
|
+
`orders_contller.rb`
|
162
|
+
|
163
|
+
```Ruby
|
164
|
+
|
165
|
+
class OrdersController < ApplicationController
|
166
|
+
|
167
|
+
before_action :authenticate_user!
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
def index
|
172
|
+
|
173
|
+
@item = Item.find(params[:item_id])
|
174
|
+
|
175
|
+
@order = Order.new(order_params)
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
if @item.order.present? || @item.user_id == current_user.id
|
180
|
+
|
181
|
+
redirect_to root_path
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
def create
|
192
|
+
|
193
|
+
@item = Item.find(params[:item_id])
|
194
|
+
|
195
|
+
@order_address = OrderAddress.new(order_params)
|
196
|
+
|
197
|
+
if @order_address.valid?
|
198
|
+
|
199
|
+
pay_item
|
200
|
+
|
201
|
+
@order_address.save
|
202
|
+
|
203
|
+
redirect_to root_path
|
204
|
+
|
205
|
+
else
|
206
|
+
|
207
|
+
render :index
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
private
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
def order_params
|
220
|
+
|
221
|
+
params.permit(:postal_code, :prefectures_id,:municipality, :address, :building, :phone_number, :item_id).merge(user_id: current_user.id, token: params[:token])
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
def pay_item
|
228
|
+
|
229
|
+
Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
|
230
|
+
|
231
|
+
Payjp::Charge.create(
|
232
|
+
|
233
|
+
amount: @item.price,
|
234
|
+
|
235
|
+
card: order_params[:token],
|
236
|
+
|
237
|
+
currency: 'jpy'
|
238
|
+
|
239
|
+
)
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
```
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
`order.rb`
|
254
|
+
|
255
|
+
```
|
256
|
+
|
257
|
+
class Order < ApplicationRecord
|
258
|
+
|
259
|
+
belongs_to :user
|
260
|
+
|
261
|
+
belongs_to :item
|
262
|
+
|
263
|
+
has_one :address
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
attr_accessor :token
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
```
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
`order_address.rb`
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
|
279
|
+
class OrderAddress
|
280
|
+
|
281
|
+
include ActiveModel::Model
|
282
|
+
|
283
|
+
attr_accessor :postal_code, :prefectures_id, :municipality, :address, :building, :phone_number, :user_id, :item_id, :token
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
with_options presence: true do
|
288
|
+
|
289
|
+
validates :user_id
|
290
|
+
|
291
|
+
validates :item_id
|
292
|
+
|
293
|
+
validates :postal_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/ }
|
294
|
+
|
295
|
+
validates :municipality
|
296
|
+
|
297
|
+
validates :address
|
298
|
+
|
299
|
+
validates :phone_number, format: {with: /\A[0-9]{10,11}\z/ }
|
300
|
+
|
301
|
+
validates :token
|
302
|
+
|
303
|
+
end
|
176
304
|
|
177
305
|
|
178
306
|
|
179
|
-
<%# 配送先の入力 %>
|
180
|
-
|
181
|
-
<div class='shipping-address-form'>
|
182
|
-
|
183
|
-
<h1 class='info-input-haedline'>
|
184
|
-
|
185
|
-
配送先入力
|
186
|
-
|
187
|
-
</h1>
|
188
|
-
|
189
|
-
<div class="form-group">
|
190
|
-
|
191
|
-
<div class='form-text-wrap'>
|
192
|
-
|
193
|
-
<label class="form-text">郵便番号</label>
|
194
|
-
|
195
|
-
<span class="indispensable">必須</span>
|
196
|
-
|
197
|
-
</div>
|
198
|
-
|
199
|
-
<%= f.text_field :postal_code, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
|
200
|
-
|
201
|
-
</div>
|
202
|
-
|
203
|
-
<div class="form-group">
|
204
|
-
|
205
|
-
<div class='form-text-wrap'>
|
206
|
-
|
207
|
-
<label class="form-text">都道府県</label>
|
208
|
-
|
209
|
-
<span class="indispensable">必須</span>
|
210
|
-
|
211
|
-
</div>
|
212
|
-
|
213
|
-
|
307
|
+
validates :prefectures_id, numericality: {other_than: 0, message: "can't be blank"}
|
214
|
-
|
215
|
-
|
308
|
+
|
216
|
-
|
217
|
-
|
309
|
+
|
218
|
-
|
219
|
-
|
310
|
+
|
220
|
-
|
221
|
-
<label class="form-text">市区町村</label>
|
222
|
-
|
223
|
-
<span class="indispensable">必須</span>
|
224
|
-
|
225
|
-
</div>
|
226
|
-
|
227
|
-
<%= f.text_field :municipality, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
|
228
|
-
|
229
|
-
</div>
|
230
|
-
|
231
|
-
<div class="form-group">
|
232
|
-
|
233
|
-
<div class='form-text-wrap'>
|
234
|
-
|
235
|
-
<label class="form-text">番地</label>
|
236
|
-
|
237
|
-
<span class="indispensable">必須</span>
|
238
|
-
|
239
|
-
</div>
|
240
|
-
|
241
|
-
<%= f.text_field :address, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
|
242
|
-
|
243
|
-
</div>
|
244
|
-
|
245
|
-
<div class="form-group">
|
246
|
-
|
247
|
-
<div class='form-text-wrap'>
|
248
|
-
|
249
|
-
<label class="form-text">建物名</label>
|
250
|
-
|
251
|
-
<span class="form-any">任意</span>
|
252
|
-
|
253
|
-
</div>
|
254
|
-
|
255
|
-
<%= f.text_field :building, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
|
256
|
-
|
257
|
-
</div>
|
258
|
-
|
259
|
-
<div class="form-group">
|
260
|
-
|
261
|
-
<div class='form-text-wrap'>
|
262
|
-
|
263
|
-
<label class="form-text">電話番号</label>
|
264
|
-
|
265
|
-
<span class="indispensable">必須</span>
|
266
|
-
|
267
|
-
</div>
|
268
|
-
|
269
|
-
<%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
|
270
|
-
|
271
|
-
</div>
|
272
|
-
|
273
|
-
</div>
|
274
|
-
|
275
|
-
<%# /配送先の入力 %>
|
276
|
-
|
277
|
-
<div class='buy-btn'>
|
278
|
-
|
279
|
-
<%= f.submit "購入" ,class:"buy-red-btn", id:"button" %>
|
280
|
-
|
281
|
-
</div>
|
282
|
-
|
283
|
-
<% end %>
|
284
|
-
|
285
|
-
</div>
|
286
|
-
|
287
|
-
</div>
|
288
|
-
|
289
|
-
<%= render "shared/second-footer"%>
|
290
|
-
|
291
|
-
```
|
292
|
-
|
293
|
-
`orders_contller.rb`
|
294
|
-
|
295
|
-
```Ruby
|
296
|
-
|
297
|
-
class OrdersController < ApplicationController
|
298
|
-
|
299
|
-
before_action :authenticate_user!
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
def
|
311
|
+
def save
|
304
|
-
|
305
|
-
|
312
|
+
|
306
|
-
|
307
|
-
@order = Order.new(order_params)
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
313
|
+
order = Order.create(user_id: user_id, item_id: item_id)
|
312
|
-
|
314
|
+
|
313
|
-
re
|
315
|
+
Address.create(postal_code: postal_code, prefectures_id: prefectures_id,municipality: municipality, address: address, building: building, phone_number: phone_number, order_id: order.id)
|
314
316
|
|
315
317
|
end
|
316
318
|
|
317
|
-
|
318
|
-
|
319
|
-
|
319
|
+
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
def create
|
324
|
-
|
325
|
-
@item = Item.find(params[:item_id])
|
326
|
-
|
327
|
-
@order_address = OrderAddress.new(order_params)
|
328
|
-
|
329
|
-
if @order_address.valid?
|
330
|
-
|
331
|
-
pay_item
|
332
|
-
|
333
|
-
@order_address.save
|
334
|
-
|
335
|
-
redirect_to root_path
|
336
|
-
|
337
|
-
else
|
338
|
-
|
339
|
-
render :index
|
340
|
-
|
341
|
-
end
|
342
|
-
|
343
|
-
end
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
private
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
def order_params
|
352
|
-
|
353
|
-
params.permit(:postal_code, :prefectures_id,:municipality, :address, :building, :phone_number, :item_id).merge(user_id: current_user.id, token: params[:token])
|
354
|
-
|
355
|
-
end
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
def pay_item
|
360
|
-
|
361
|
-
Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
|
362
|
-
|
363
|
-
Payjp::Charge.create(
|
364
|
-
|
365
|
-
amount: @item.price,
|
366
|
-
|
367
|
-
card: order_params[:token],
|
368
|
-
|
369
|
-
currency: 'jpy'
|
370
|
-
|
371
|
-
)
|
372
|
-
|
373
|
-
end
|
374
|
-
|
375
|
-
|
376
320
|
|
377
321
|
end
|
378
322
|
|
379
|
-
|
380
|
-
|
381
|
-
```
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
`order.rb`
|
386
|
-
|
387
|
-
```
|
388
|
-
|
389
|
-
class Order < ApplicationRecord
|
390
|
-
|
391
|
-
belongs_to :user
|
392
|
-
|
393
|
-
belongs_to :item
|
394
|
-
|
395
|
-
has_one :address
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
attr_accessor :token
|
400
|
-
|
401
|
-
end
|
402
|
-
|
403
|
-
```
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
`order_address.rb`
|
408
|
-
|
409
|
-
```ruby
|
410
|
-
|
411
|
-
class OrderAddress
|
412
|
-
|
413
|
-
include ActiveModel::Model
|
414
|
-
|
415
|
-
attr_accessor :postal_code, :prefectures_id, :municipality, :address, :building, :phone_number, :user_id, :item_id, :token
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
with_options presence: true do
|
420
|
-
|
421
|
-
validates :user_id
|
422
|
-
|
423
|
-
validates :item_id
|
424
|
-
|
425
|
-
validates :postal_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/ }
|
426
|
-
|
427
|
-
validates :municipality
|
428
|
-
|
429
|
-
validates :address
|
430
|
-
|
431
|
-
validates :phone_number, format: {with: /\A[0-9]{10,11}\z/ }
|
432
|
-
|
433
|
-
validates :token
|
434
|
-
|
435
|
-
end
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
validates :prefectures_id, numericality: {other_than: 0, message: "can't be blank"}
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
def save
|
444
|
-
|
445
|
-
order = Order.create(user_id: user_id, item_id: item_id)
|
446
|
-
|
447
|
-
Address.create(postal_code: postal_code, prefectures_id: prefectures_id,municipality: municipality, address: address, building: building, phone_number: phone_number, order_id: order.id)
|
448
|
-
|
449
|
-
end
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
end
|
454
|
-
|
455
323
|
```
|
456
324
|
|
457
325
|
### 試したこと
|
458
326
|
|
459
327
|
|
460
328
|
|
329
|
+
renderメソッドを使用すればいいと聞いていましたので使用したのですがダメでした。
|
330
|
+
|
461
|
-
色々と調べてみたのですが解決につながりそうな情報を得ることができませんでした。
|
331
|
+
それ以外で色々と調べてみたのですが解決につながりそうな情報を得ることができませんでした。
|
462
332
|
|
463
333
|
|
464
334
|
|