購入機能の実装のところで、購入情報をDBに保存したいのですが、そのままリダイレクトせずトップページに遷移せず、そのページに残ってしまいます。User must exist, Item must existtoiuというエラーメッセージがブラウザ上に表示されるだけの状態になってしまいます。
どなたかご教授願えますでしょうか?よろしくお願いします。
app/controllers/purchase_history_controller.rb class PurchaseHistoriesController < ApplicationController def index @user_item = UserItem.new @item = Item.find(params[:item_id]) end def create @item = Item.find(params[:item_id]) @user_item = UserItem.new(purchase_history_params) @user_item = SendingDestination.create @user_item = PurchaseHistory.create if @user_item.valid? @user_item.save redirect_to root_path else render action: :index end end private def purchase_history_params params.permit(:nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :birth_day, :image, :name, :introduction, :category_id, :item_condition_id, :postage_id, :prefecture_id, :prepare_id, :price, :post_code, :prefecture_id, :city, :house_number, :building_name, :phone_number) end end
app/models/user_item.rb class UserItem include ActiveModel::Model attr_accessor :nickname, :email, :encrypted_password, :first_name, :family_name, :first_name_kana, :family_name_kana, :birth_day, :image, :name, :introduction, :category_id, :item_condition_id, :postage_id, :prefecture_id, :prepare_id, :price, :post_code, :prefecture_id, :city, :house_number, :building_name, :phone_number 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 validates :image validates :name validates :introduction validates :price validates :post_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)"} validates :prefecture_id, numericality: { other_than: 0, message: "Select" } validates :city validates :house_number validates :phone_number, numericality: { only_integer: true, message: "Input only number" } validates :user validates :item end validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999, message: "Out of setting range"} validates :price, numericality: { only_integer: true, message: "Half-width number." } with_options numericality: { other_than: 0, message: "Select" } do validates :item_condition_id validates :postage_id validates :prefecture_id validates :prepare_id validates :category_id end def save user = User.create(nickname: nickname, email: email, encrypted_password: encrypted_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) Item.create(image: image, name: name, introduction: introduction, category_id: category_id, item_condition_id: item_condition_id, postage_id: postage_id, prefecture_id: prefecture_id, prepare_id: prepare_id, price: price, user_id: user.id) 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) PurchaseHistory.create(user_id: user.id, item_id: item.id) end end
config/routes.rb Rails.application.routes.draw do devise_for :users root "items#index" resources :items do resources :purchase_histories, only: [:index, :create] end end
以下他のモデルです。
app/models/sendingdestination.rb class SendingDestination < ApplicationRecord belongs_to :purchase_history, optional: true with_options presence: true do validates :post_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)"} validates :prefecture_id, numericality: { other_than: 0, message: "Select" } validates :city validates :house_number validates :phone_number, numericality: { only_integer: true, message: "Input only number" } end end
app/models/purchase_history.rb class PurchaseHistory < ApplicationRecord belongs_to :user, optional: true belongs_to :item, optional: true has_one :sending_destination end
試したこと
デバッック(binding.pry)を利用して保存する情報がパラメーターに存在しているのかどうか見てみましたが、ちゃんと記録されているみたいでした。
条件分技のifの処理がtrueにならず、うまく行われていないのが原因みたいです。
optinal:trueを購入履歴のモデルファイルにつけくわてみました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。