0W5E8fPq1EOm4yE
@0W5E8fPq1EOm4yE (Nishio)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

住所情報に関する情報を購入の都度入力させること

現在、フリマアプリの購入機能の実装を行っておりますが,現状では見本の通りに正しく情報が入力されていないと購入ページに戻り、再度書き直しさせるといったことができない状況です。どうしたら見本通りにできますでしょうか?

目標
https://gyazo.com/d512d344d79537c8ed612f7767f668c4

現在の状態
https://gyazo.com/75b90aebf99f6d3d4f8ea0f836a0086c

class AddressesController < ApplicationController
  before_action :authenticate_user!,
  def index
    @purchase_address = PurchaseAddress.new
    @item = Item.find(params[:item_id])
    if current_user == @item.user || @item.purchase.present?
      redirect_to root_path
      end
  end


  def create
    @item = Item.find(params[:item_id])
    @purchase_address = PurchaseAddress.new(address_params)
    if @purchase_address.valid?
      @purchase_address.save
      pay_item
      redirect_to root_path
    else
      render action: :index

    end
  end

  private

  def address_params
    params.require(:purchase_address).permit(:post_number, :prefecture_id, :municipality, :addresses, :build_name ,:phone_number).merge(user_id: current_user.id,item_id: params[:item_id],token: params[:token])
  end



  def pay_item
    Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
    Payjp::Charge.create(
      amount: @item.price,
      card: address_params[:token],
      currency:'jpy'
    )
  end

end
class PurchaseAddress
  include ActiveModel::Model
  attr_accessor :post_number,:prefecture_id, :addresses, :municipality, :prefecture_id, :phone_number, :build_name, :user_id, :item_id ,:purchase_id ,:token



  with_options presence: true do
    validates :post_number, format: { with: /\A\d{3}[-]\d{4}\z/, message: 'Input correctly' }
    validates :municipality
    validates :prefecture_id, numericality: { other_than: 1, message: 'Select' }
    validates :addresses
    validates :phone_number, format: { with: /\A\d{11}\z/, message: 'Input only number' }
    validates :token
  end

  def save
  @purchase_address = Purchase.create(user_id: user_id,item_id: item_id)
  Address.create(post_number: post_number, municipality: municipality, prefecture_id: prefecture_id, addresses: addresses, build_name: build_name, phone_number: phone_number, purchase_id: @purchase_address.id )
  end
end

<%= render "shared/second-header"%>
<div class='transaction-contents'>
  <div class='transaction-main'>
    <h1 class='transaction-title-text'>
      購入内容の確認
    </h1>
    <%# 購入内容の表示 %>
    <div class='buy-item-info'>
      <%= image_tag @item.image, class: 'buy-item-img' %>
      <div class='buy-item-right-content'>
        <h2 class='buy-item-text'>
          <%= @item.name %>
        </h2>
        <div class='buy-item-price'>
          <p class='item-price-text'>¥<%= @item.price %></p>
          <p class='item-price-sub-text'><%= @item.burden.name %></p>
        </div>
      </div>
    </div>
    <%# /購入内容の表示 %>

    <%# 支払額の表示 %>
    <div class='item-payment'>
      <h1 class='item-payment-title'>
        支払金額
      </h1>
      <p class='item-payment-price'>
        ¥<%= @item.price %>
      </p>
    </div>
    <%# /支払額の表示 %>
    <%= form_with  id: 'charge-form', class: 'transaction-form-wrap',url:item_addresses_path, model: @purchase_address,local: true do |f| %>
    <%= render 'shared/error_messages', model: f.object %>
    <%# カード情報の入力 %>
    <div class='credit-card-form'>
      <h1 class='info-input-haedline'>
        クレジットカード情報入力
      </h1>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">カード情報</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16" %>
        <div class='available-card'>
          <%= image_tag 'card-visa.gif', class: 'card-logo'%>
          <%= image_tag 'card-mastercard.gif', class: 'card-logo'%>
          <%= image_tag 'card-jcb.gif', class: 'card-logo'%>
          <%= image_tag 'card-amex.gif', class: 'card-logo'%>
        </div>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">有効期限</label>
          <span class="indispensable">必須</span>
        </div>
        <div class='input-expiration-date-wrap'>
          <%= f.text_area :exp_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3" %>
          <p>月</p>
          <%= f.text_area :exp_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23" %>
          <p>年</p>
        </div>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">セキュリティコード</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.text_field :cvc,class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4" %>
      </div>
    </div>

    <%# /カード情報の入力 %>

    <%# 配送先の入力 %>
    <div class='shipping-address-form'>
      <h1 class='info-input-haedline'>
        配送先入力
      </h1>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">郵便番号</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.text_field :post_number, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">都道府県</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.collection_select(:prefecture_id, Prefecture.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">市区町村</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.text_field :municipality, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">番地</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.text_field :addresses, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">建物名</label>
          <span class="form-any">任意</span>
        </div>
        <%= f.text_field :build_name, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
      </div>
      <div class="form-group">
        <div class='form-text-wrap'>
          <label class="form-text">電話番号</label>
          <span class="indispensable">必須</span>
        </div>
        <%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
      </div>
    </div>
    <%# /配送先の入力 %>
    <div class='buy-btn'>
      <%= f.submit "購入" ,class:"buy-red-btn" %>
    </div>
    <% end %>

  </div>
</div>
<%= render "shared/second-footer"%>

試したこと
コントローラーの設定render action: :indexからredirect_to item_addresses_pathに変更したがエラーメッセージが表示されなかった。

0

No Answers yet.

Your answer might help someone💌