購入機能を実装中です。エラーを探しきれず困っています。
ビュー
<%= form_with model: (@userdonation, item_purchases_path, id: 'charge-form', class: 'transaction-form-wrap', local: true) do |f| %> <%= render partial: 'shared/error_messages', locals: {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 'hoge', 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 'hoge', class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3" %> <p>月</p> <%= f.text_area 'hoge', 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 "hoge",class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4" %> </div> </div> <%# /カード情報の入力 %>
Rails.application.routes.draw do devise_for :users root to: "items#index" resources :items, only: [:new, :create, :show, :edit, :update, :destroy] do resources :purchases, only:[:index, :new, :create] end end
class UserDonation include ActiveModel::Model attr_accessor :post_code, :city, :block_number, :building_name, :phone_number, :prefecture_id, :purchases_id, :item_id, :user_id with_options presence: true do validates :city, :block_number, :phone_number, :prefecture_id validates :post_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)"} validates :region_id, numericality: { other_than: 0, message: "can't be blank" } end def save Address.create(post_code: post_code, city: city, block_number: block_number, building_name: building_name, phone_number: phone_number, prefecture_id: prefecture_id, purchases_id: purchases_id) Purchase.create(item_id: item_id, user_id: user_id) end end
class PurchasesController < ApplicationController def index end def new @userdonation = UserDonation.new end def create @item = Item.find(params[:id]) @userdonation = UserDonation.new(purchase_params) if @userdonation.valid? @userdonation.save redirect_to action: :index else render action: :new end end private def purchase_params params.require(:user_donation).permit(:post_code, :city, :block_number, :building_name, :phone_number, :prefecture_id).merge(user_id: current_user.id, item_id: params[:item_id]) end end
class Purchase < ApplicationRecord belongs_to :user belongs_to :item has_one :address end
class Address < ApplicationRecord extend ActiveHash::Associations::ActiveRecordExtensions![イメージ説明](703fa421fc100479665e995c58224f50.png) belongs_to_active_hash :region belongs_to :purchase end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/02 15:25
2020/11/02 15:29
2020/11/02 15:48
2020/11/02 15:52
2020/11/02 16:01
2020/11/02 16:04
2020/11/02 16:50
2020/11/02 16:58
2020/11/02 17:12
2020/11/02 23:07
2020/11/02 23:47
2020/11/03 01:32
2020/11/03 01:39
2020/11/03 01:47
2020/11/03 01:50