エラー文ではコントローラーのindexアクションの定義がなされていないということで、購入編集ページに遷移できないでいます。使用しているインスタンス変数といたしましては、「formオブジェクトパターン」を利用したものになっています。formオブジェクト使用のモデルの定義も一通りできていると思いますが、うまくできません。どなたかご教授願います。
NoMethodError in PurchaseHistoriesController#index undefined method `all' for UserItem:Class
app/controllers/purchase_histories_controller.rb class PurchaseHistoriesController < ApplicationController def index @user_purchase_histories = UserPurchaseHistory.all end def new @user_purchase_history = UserPurchaseHistory.new end def create @user_purchase_history = UserPurchaseHistory.new(purchase_history_params) if @user_purchase_history.valid? @user_purchase_history.save redirect_to action: :index else render action: :new end end private def purchase_history_params params.require(:user_purchase_history).permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana) end end
views/parchase_history/index.html.erb <%= 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-sample.png", class: 'buy-item-img' %> <div class='buy-item-right-content'> <h2 class='buy-item-text'> <%= "商品名" %> </h2> <div class='buy-item-price'> <p class='item-price-text'>¥<%= 999,999,999 %></p> <p class='item-price-sub-text'><%= '配送料負担' %></p> </div> </div> </div> <%# /購入内容の表示 %> <%# 支払額の表示 %> <div class='item-payment'> <h1 class='item-payment-title'> 支払金額 </h1> <p class='item-payment-price'> ¥<%= "販売価格" %> </p> </div> <%# /支払額の表示 %> <%= form_with model: @user_purchase_history, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %> <%# カード情報の入力 %> <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_code, 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 :city, 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 :house_number, 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 :building_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"%>
config/routes.rb Rails.application.routes.draw do devise_for :users root "items#index" resources :items do resources :purchase_histories, only: [:index, :new, :create] end end
以下formオブジェクトのモデルファイルになります。
app/models/user_purchase_history.rb class UserPurchaseHistory include ActiveModel::Model attr_accessor :nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :birth_day with_options presence: true do validates :nickname validates :email validates :password, format: { with: /\A[a-zA-Z0-9]+\z/, message: "Include both letters and numbers."} validates :first_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."} validates :first_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."} validates :family_name, format: { with: /\A[ぁ-んァ-ン一-龥]/, message: "Full-width characters."} validates :family_name_kana, format: { with: /\A[ァ-ヶー-]+\z/, message: "Full-width katakana characters."} validates :birth_day end def save user = User.create(nickname: nickname, email: email, password: password, first_name: first_name, family_name: family_name, first_name_kana: first_name_kana, family_name_kana: family_name_kana, birth_day: birth_day) SendingDestination.create(post_code: post_code, prefecture_id: prefecture.id, city: city, house_number: house_number, building_name: building_name, phone_number: phone_number, purchase_history_id: purchase_history.id) Purchase_history.create(user_id: user.id, item_id: item.id) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。