質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

3044閲覧

購入機能画面に商品情報が反映されない

murohi-08

総合スコア12

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/10 17:28

編集2020/12/11 03:34

商品情報を購入画面に表示させたいのですが、メソッドが空であったり引数エラーであったりで困惑しています。
どなたかご教授ください。よろしくお願いします。
イメージ説明
イメージ説明
イメージ説明

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 "", class: 'buy-item-img' %> <div class='buy-item-right-content'> <h2 class='buy-item-text'> <%= @user_item.name %> </h2> <div class='buy-item-price'> <p class='item-price-text'>¥<%= @user_item.price %></p> <p class='item-price-sub-text'><%= @user_item.prepare.name %></p> </div> </div> </div> <%# /購入内容の表示 %> <%# 支払額の表示 %> <div class='item-payment'> <h1 class='item-payment-title'> 支払金額 </h1> <p class='item-payment-price'> ¥<%= @user_item.price %> </p> </div> <%# /支払額の表示 %> <%= form_with model: @user_item, url: item_purchase_histories_path, 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"%>
app/controllers/purchase_histories_controller.rb class PurchaseHistoriesController < ApplicationController def index @user_item = UserItem.new end def create @user_item = UserItem.new(purchase_history_params) if @user_item.valid? @user_item.save redirect_to action: :index else render action: :new end end private def purchase_history_params params.require(:user_item).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

以下Formオブジェクトのモデルファイルです。

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 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) SendingDestination.create(post_code: post_code, prefecture_id: prefecture, 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) 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) end end
app/models/item.rb class Item < ApplicationRecord belongs_to :user has_one :purchase_history has_one_attached :image extend ActiveHash::Associations::ActiveRecordExtensions belongs_to_active_hash :prefecture belongs_to_active_hash :prepare belongs_to_active_hash :postage belongs_to_active_hash :item_condition belongs_to_active_hash :category with_options presence: true do validates :image validates :name validates :introduction validates :price 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 end

試したこと
画像のコードを記述する際には@user_item.imageをimage_tagの中に入れましたが、引数エラーが出ます。
価格に関するコードは@user_item.priceですが、@を抜いてもエラーにはならないのですが何も表示されません。
配送料負担のところは@user_item.prepare.nameはメソッド空のエラーが表示され、nameを_idにするとエラーは出ませんが表示がされません。
おそらくはフォームオブジェクトのファイルに問題があると思っているのですが、原因が特定できません。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

purchase_histories_controller.rbのindexアクションに @item = Item.find(params[:item_id])を加えたら行けました。

投稿2020/12/11 06:51

murohi-08

総合スコア12

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

恐らくですが、
モデルにアソシエーションが書かれていないのでそれが原因かと思います。
お確かめ下さい。

@user_item.imageをimage_tagの中に入れましたが

複数枚のimageがuser_itemに紐付くのでしたら、

= image_tag @user_item.images[0].image

などになるかと思います。
こちらもアソシエーションは必要です。

如何でしょうか?

投稿2020/12/10 22:14

tomtomtomtom

総合スコア563

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

murohi-08

2020/12/11 02:24

ご回答ありがとうございます。formオブジェクトではアソシエーションは組めないみたいで、@user_item.images[0].image を記述してもNomethodエラーになってしまいます。空になっている原因が特定できればいいのですが、他のどのファイルをお見せすればわかりますでしょうか?
tomtomtomtom

2020/12/11 03:30

ご希望に添えず申し訳ありませんでした。 モデルファイルの方にはアソシエーションは既にあるということでしょうか? よろしければアソシエーションの記述をお見せいただくことは可能でしょうか? モデルファイルの中にあると思います。
murohi-08

2020/12/11 03:36

itemモデルを付け加えました。ご確認ください。 自分はおそらくformオブジェクトの組み方が間違っていると思っています。
tomtomtomtom

2020/12/11 09:53

問題解決されたようで良かったです。 なんのお役にも立てずに誠に申し訳ありませんでした。
murohi-08

2020/12/11 10:03

いえ、お答えくださっただけでも幸いです。 まだまだエラーが発生すると思うのでその時はどうぞよろしくお願いします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問