質問編集履歴

2

Routesを追記しました。

2021/12/21 10:58

投稿

mayugevohn
mayugevohn

スコア0

test CHANGED
File without changes
test CHANGED
@@ -130,7 +130,7 @@
130
130
 
131
131
 
132
132
 
133
- <%= form_with model: @order_address,url: item_orders_path(@item.id) , id: 'charge-form',local: true do |f| %>
133
+ <%= form_with model: @order_address,url: item_orders_path, id: 'charge-form',local: true do |f| %>
134
134
 
135
135
 
136
136
 
@@ -318,9 +318,15 @@
318
318
 
319
319
 
320
320
 
321
- <div class="submit-btn">
321
+ <div class="btn-contents">
322
-
322
+
323
- <%= link_to "購入する", item_orders_path(@item.id) %>
323
+ <%= f.submit "購入する", class:'submit-btn' %>
324
+
325
+ </div>
326
+
327
+ <div>
328
+
329
+ <%=link_to 'もどる', :back, class:"back-btn" %>
324
330
 
325
331
  </div>
326
332
 
@@ -330,30 +336,20 @@
330
336
 
331
337
  </div>
332
338
 
333
-
339
+ </div>
340
+
341
+ </div>
342
+
343
+ ```
344
+
345
+
346
+
347
+ ```html.erb
348
+
349
+ <% if model.errors.any? %>
334
350
 
335
351
  <div>
336
352
 
337
- <%=link_to 'もどる', :back, class:"back-btn" %>
338
-
339
- </div>
340
-
341
-
342
-
343
- </div>
344
-
345
- </div>
346
-
347
- ```
348
-
349
-
350
-
351
- ```html.erb
352
-
353
- <% if model.errors.any? %>
354
-
355
- <div>
356
-
357
353
  <ul>
358
354
 
359
355
  <% model.errors.full_messages.each do |message| %>
@@ -509,3 +505,39 @@
509
505
  end
510
506
 
511
507
  ```
508
+
509
+
510
+
511
+ ### Routes(追記)
512
+
513
+ ```ruby
514
+
515
+ Rails.application.routes.draw do
516
+
517
+ devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' }
518
+
519
+ root to: 'users#new'
520
+
521
+ resources :users, only: %i[show new]
522
+
523
+
524
+
525
+ resources :items, only: %i[index new create show destroy] do
526
+
527
+ resources :orders, only: %i[index create]
528
+
529
+ end
530
+
531
+
532
+
533
+ resources :babies, only: %i[new create] do
534
+
535
+ resources :articles, only: %i[index show new create destroy]
536
+
537
+ end
538
+
539
+
540
+
541
+ end
542
+
543
+ ```

1

Controllerを追記しました。

2021/12/21 10:58

投稿

mayugevohn
mayugevohn

スコア0

test CHANGED
File without changes
test CHANGED
@@ -423,3 +423,89 @@
423
423
  <%= %>のイコールが抜けていないことは確認しました。
424
424
 
425
425
  意図的にバリデーションエラーが発生する状況で試しましたが、これでもうまく表示されません。
426
+
427
+
428
+
429
+ ### Controller(追記)
430
+
431
+
432
+
433
+
434
+
435
+ ```Ruby
436
+
437
+ class OrdersController < ApplicationController
438
+
439
+
440
+
441
+ def index
442
+
443
+ @item = Item.find(params[:item_id])
444
+
445
+ @order_address = OrderAddress.new
446
+
447
+ end
448
+
449
+
450
+
451
+ def create
452
+
453
+ @item = Item.find(params[:item_id])
454
+
455
+ @order_address = OrderAddress.new(order_params)
456
+
457
+ if @order_address.valid?
458
+
459
+ pay_item
460
+
461
+ @order_address.save
462
+
463
+ return redirect_to user_path(current_user.id)
464
+
465
+ else
466
+
467
+ @order_address = OrderAddress.new(order_params)
468
+
469
+ render :index
470
+
471
+ end
472
+
473
+ end
474
+
475
+
476
+
477
+
478
+
479
+ private
480
+
481
+
482
+
483
+ def order_params
484
+
485
+ params.require(:order_address).permit(:zip_code, :prefecture_id, :city, :address, :building_name, :phone_number).merge(item_id: params[:item_id],user_id: current_user.id,token: params[:token])
486
+
487
+ end
488
+
489
+
490
+
491
+ def pay_item
492
+
493
+ Payjp.api_key = ENV['PAYJP_SECRET_KEY']
494
+
495
+ Payjp::Charge.create(
496
+
497
+ amount: @item.price,
498
+
499
+ card: order_params[:token],
500
+
501
+ currency: 'jpy'
502
+
503
+ )
504
+
505
+ end
506
+
507
+
508
+
509
+ end
510
+
511
+ ```