前提・実現したいこと
ruby on railsを用いてオリジナルアプリを作成しています。
クレジットカード決済機能を実装しようとしていますが、その際にエラーメッセージが出力されるようにしたい状況です。
発生している問題・エラーメッセージ
下記コードの通りエラーメッセージの読み込みをしておりますが、エラーメッセージが表示されない状況です。
情報がDBに保存されていない状況であることから、何某かのエラーは出ているはずであり、ターミナルでもエラーの出力がされている記録があります。
Started GET "/items/5/orders" for ::1 at 2021-12-19 19:44:48 +0900 Processing by OrdersController#index as HTML Parameters: {"item_id"=>"5"} Item Load (0.7ms) SELECT `items`.* FROM `items` WHERE `items`.`id` = 5 LIMIT 1 ↳ app/controllers/orders_controller.rb:4:in `index' Rendering orders/index.html.erb within layouts/application User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/views/shared/_header.html.erb:8 Rendered shared/_header.html.erb (Duration: 7.6ms | Allocations: 1709) ActiveStorage::Attachment Load (0.6ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 5 AND `active_storage_attachments`.`record_type` = 'Item' AND `active_storage_attachments`.`name` = 'image' LIMIT 1 ↳ app/views/orders/index.html.erb:10 ActiveStorage::Blob Load (0.6ms) SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 14 LIMIT 1 ↳ app/views/orders/index.html.erb:10 Rendered shared/_error_messages.html.erb (Duration: 0.1ms | Allocations: 14) Rendered orders/index.html.erb within layouts/application (Duration: 26.1ms | Allocations: 7934) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 51ms (Views: 45.0ms | ActiveRecord: 2.7ms | Allocations: 23764)
該当のソースコード
html.erb
1<%= render "shared/header" %> 2 3<div class="main"> 4 <div class="user__wrapper"> 5 <div class="page-heading"> 6 <h2>購入ページ</h2> 7 </div> 8 9<div class="content-img"> 10 <%= image_tag @item.image ,class:"content-box-img" %> 11</div> 12 13<div class="registration-main"> 14 <h1 class="weight-bold-text"> 15 ・お下がりの品物 16 </h1> 17 <p> 18 <%= @item.item_name %> 19 </p> 20 <h1 class="weight-bold-text"> 21 ・支払金額 22 </h1> 23 <p> 24 <%= @item.price %>円 25 </p> 26 <h1 class="weight-bold-text"> 27 ・送料負担 28 </h1> 29 <p> 30 <%= @item.send_fee.name %> 31 </p> 32 33<%= form_with model: @order_address,url: item_orders_path, id: 'charge-form',local: true do |f| %> 34 35<%= render 'shared/error_messages', model: f.object %> 36 37<div class='credit-card-form'> 38 <h1 class="segment"> 39 クレジットカード情報入力 40 </h1> 41 <div class="form-group"> 42 <div class="weight-bold-text"> 43 <label>カード情報</label> 44 <span class="indispensable">必須</span> 45 </div> 46 <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16"%> 47 <div class='available-card'> 48 <%= image_tag 'card-visa.gif', class: 'card-logo'%> 49 <%= image_tag 'card-mastercard.gif', class: 'card-logo'%> 50 <%= image_tag 'card-jcb.gif', class: 'card-logo'%> 51 <%= image_tag 'card-amex.gif', class: 'card-logo'%> 52 </div> 53 </div> 54 <div class="form-group"> 55 <div class="weight-bold-text"> 56 <label>有効期限</label> 57 <span class="indispensable">必須</span> 58 </div> 59 <div class='input-expiration-date-wrap'> 60 <%= f.text_area :exp_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3"%> 61 <p>月</p> 62 <%= f.text_area :exp_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23" %> 63 <p>年</p> 64 </div> 65 </div> 66 <div class="form-group"> 67 <div class="weight-bold-text"> 68 <label>セキュリティコード</label> 69 <span class="indispensable">必須</span> 70 </div> 71 <%= f.text_field :cvc, class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4" %> 72 </div> 73</div> 74 75<h1 class="segment"> 76 発送情報入力 77</h1> 78 79<div class="form-group"> 80 <div class="weight-bold-text"> 81 <label class="form-text">郵便番号</label> 82 <span class="indispensable">必須</span> 83 </div> 84 <%= f.text_field :zip_code, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %> 85</div> 86 87<div class="form-group"> 88 <div class='weight-bold-text'> 89 <label class="form-text">都道府県</label> 90 <span class="indispensable">必須</span> 91 </div> 92 <%= f.collection_select(:prefecture_id, Prefecture.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %> 93</div> 94 95<div class="form-group"> 96 <div class='weight-bold-text'> 97 <label class="form-text">市区町村</label> 98 <span class="indispensable">必須</span> 99 </div> 100 <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%> 101</div> 102 103<div class="form-group"> 104 <div class='weight-bold-text'> 105 <label class="form-text">番地</label> 106 <span class="indispensable">必須</span> 107 </div> 108 <%= f.text_field :address, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%> 109</div> 110 111<div class="form-group"> 112 <div class='weight-bold-text'> 113 <label class="form-text">建物名</label> 114 <span class="form-any">任意</span> 115 </div> 116 <%= f.text_field :building_name, class:"input-default", id:"building", placeholder:"例)柳ビル103"%> 117</div> 118 119<div class="form-group"> 120 <div class='weight-bold-text'> 121 <label class="form-text">電話番号</label> 122 <span class="indispensable">必須</span> 123 </div> 124 <%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%> 125</div> 126 127<div class="btn-contents"> 128 <%= f.submit "購入する", class:'submit-btn' %> 129</div> 130<div> 131 <%=link_to 'もどる', :back, class:"back-btn" %> 132</div> 133 134<% end %> 135</div> 136 </div> 137</div>
html.erb
1<% if model.errors.any? %> 2<div> 3 <ul> 4 <% model.errors.full_messages.each do |message| %> 5 <li><%= message %></li> 6 <% end %> 7 </ul> 8</div> 9<% end %>
バリデーションの設定
ruby
1 include ActiveModel::Model 2 attr_accessor :zip_code,:prefecture_id,:city,:address,:building_name,:phone_number,:token,:user_id,:item_id,:token 3 4 with_options presence: true do 5 validates :user_id 6 validates :item_id 7 validates :zip_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)"} 8 validates :token 9 validates :zip_code 10 validates :city 11 validates :address 12 validates :phone_number,format: {with: /\A\d{10,11}\z/, message: "is invalid."} 13 end 14 validates :prefecture_id, numericality: {other_than: 0, message: "can't be blank"} 15 16 def save 17 order = Order.create(user_id: user_id, item_id: item_id) 18 Address.create(zip_code: zip_code, prefecture_id: prefecture_id, city: city, address: address, building_name: building_name, phone_number: phone_number,order_id: order.id) 19 end
試したこと
<%= %>のイコールが抜けていないことは確認しました。
意図的にバリデーションエラーが発生する状況で試しましたが、これでもうまく表示されません。
Controller(追記)
Ruby
1class OrdersController < ApplicationController 2 3 def index 4 @item = Item.find(params[:item_id]) 5 @order_address = OrderAddress.new 6 end 7 8 def create 9 @item = Item.find(params[:item_id]) 10 @order_address = OrderAddress.new(order_params) 11 if @order_address.valid? 12 pay_item 13 @order_address.save 14 return redirect_to user_path(current_user.id) 15 else 16 @order_address = OrderAddress.new(order_params) 17 render :index 18 end 19 end 20 21 22 private 23 24 def order_params 25 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]) 26 end 27 28 def pay_item 29 Payjp.api_key = ENV['PAYJP_SECRET_KEY'] 30 Payjp::Charge.create( 31 amount: @item.price, 32 card: order_params[:token], 33 currency: 'jpy' 34 ) 35 end 36 37end
Routes(追記)
ruby
1Rails.application.routes.draw do 2 devise_for :users, controllers: { registrations: 'users/registrations', sessions: 'users/sessions' } 3 root to: 'users#new' 4 resources :users, only: %i[show new] 5 6 resources :items, only: %i[index new create show destroy] do 7 resources :orders, only: %i[index create] 8 end 9 10 resources :babies, only: %i[new create] do 11 resources :articles, only: %i[index show new create destroy] 12 end 13 14end