teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

他のモデルも付け加えました。

2020/12/12 05:17

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  購入機能の実装のところで、購入情報をDBに保存したいのですが、そのままリダイレクトせずトップページに遷移せず、そのページに残ってしまいます。User must exist, Item must existtoiuというエラーメッセージがブラウザ上に表示されるだけの状態になってしまいます。
2
2
  どなたかご教授願えますでしょうか?よろしくお願いします。
3
- ![イメージ説明](da1d90dd346a64558c8336350550f25e.png)
3
+ ![イメージ説明](21468bbdb54ce863560b123ebe77274a.png)
4
4
  ```ここに言語を入力
5
5
  app/controllers/purchase_history_controller.rb
6
6
 
@@ -96,143 +96,35 @@
96
96
 
97
97
  ```
98
98
 
99
- ```ここに言語を入力
99
+ 以下他のモデルです。
100
+ ```
100
- app/views/parchase_history.html.erb
101
+ app/models/sendingdestination.rb
101
102
 
102
- <%= render "shared/second-header"%>
103
+ class SendingDestination < ApplicationRecord
104
+ belongs_to :purchase_history, optional: true
105
+ with_options presence: true do
106
+ validates :post_code, format: {with: /\A[0-9]{3}-[0-9]{4}\z/, message: "is invalid. Include hyphen(-)"}
107
+ validates :prefecture_id, numericality: { other_than: 0, message: "Select" }
108
+ validates :city
109
+ validates :house_number
110
+ validates :phone_number, numericality: { only_integer: true, message: "Input only number" }
111
+ end
112
+ end
113
+ ```
103
114
 
104
- <div class='transaction-contents'>
105
- <div class='transaction-main'>
106
- <h1 class='transaction-title-text'>
107
- 購入内容の確認
108
- </h1>
115
+ ```ここに言語を入力
109
- <%# 購入内容の表示 %>
110
- <div class='buy-item-info'>
116
+ app/models/purchase_history.rb
111
- <%= image_tag @item.image, class: 'buy-item-img' %>
112
- <div class='buy-item-right-content'>
113
- <h2 class='buy-item-text'>
114
- <%= @item.name %>
115
- </h2>
116
- <div class='buy-item-price'>
117
- <p class='item-price-text'>¥<%= @item.price %></p>
118
117
 
119
- <p class='item-price-sub-text'><%= @item.postage.name %></p>
120
- </div>
121
- </div>
122
- </div>
123
- <%# /購入内容の表示 %>
124
-
125
- <%# 支払額の表示 %>
126
- <div class='item-payment'>
118
+ class PurchaseHistory < ApplicationRecord
127
- <h1 class='item-payment-title'>
128
- 支払金額
129
- </h1>
130
- <p class='item-payment-price'>
119
+ belongs_to :user, optional: true
131
- ¥<%= @item.price %>
120
+ belongs_to :item, optional: true
132
- </p>
133
- </div>
134
- <%# /支払額の表示 %>
121
+ has_one :sending_destination
135
-
136
- <%= form_with model: @user_item, url: item_purchase_histories_path, id: 'charge-form', class: 'transaction-form-wrap',local: true do |f| %>
122
+
137
- <%# カード情報の入力 %>
138
- <div class='credit-card-form'>
139
- <h1 class='info-input-haedline'>
140
- クレジットカード情報入力
141
- </h1>
142
- <div class="form-group">
143
- <div class='form-text-wrap'>
144
- <label class="form-text">カード情報</label>
145
- <span class="indispensable">必須</span>
146
- </div>
147
- <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16" %>
148
- <div class='available-card'>
149
- <%= image_tag 'card-visa.gif', class: 'card-logo'%>
150
- <%= image_tag 'card-mastercard.gif', class: 'card-logo'%>
151
- <%= image_tag 'card-jcb.gif', class: 'card-logo'%>
152
- <%= image_tag 'card-amex.gif', class: 'card-logo'%>
153
- </div>
154
- </div>
155
- <div class="form-group">
156
- <div class='form-text-wrap'>
157
- <label class="form-text">有効期限</label>
158
- <span class="indispensable">必須</span>
159
- </div>
160
- <div class='input-expiration-date-wrap'>
161
- <%= f.text_area :exp_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3" %>
162
- <p>月</p>
163
- <%= f.text_area :exp_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23" %>
164
- <p>年</p>
165
- </div>
166
- </div>
167
- <div class="form-group">
168
- <div class='form-text-wrap'>
169
- <label class="form-text">セキュリティコード</label>
170
- <span class="indispensable">必須</span>
171
- </div>
172
- <%= f.text_field :cvc,class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4" %>
173
- </div>
174
- </div>
175
- <%# /カード情報の入力 %>
176
-
177
- <%# 配送先の入力 %>
178
- <div class='shipping-address-form'>
179
- <h1 class='info-input-haedline'>
180
- 配送先入力
123
+ end
181
- </h1>
182
- <div class="form-group">
183
- <div class='form-text-wrap'>
184
- <label class="form-text">郵便番号</label>
185
- <span class="indispensable">必須</span>
186
- </div>
187
- <%= f.text_field :post_code, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
188
- </div>
189
- <div class="form-group">
190
- <div class='form-text-wrap'>
191
- <label class="form-text">都道府県</label>
192
- <span class="indispensable">必須</span>
193
- </div>
194
- <%= f.collection_select(:prefecture_id, Prefecture.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
195
- </div>
196
- <div class="form-group">
197
- <div class='form-text-wrap'>
198
- <label class="form-text">市区町村</label>
199
- <span class="indispensable">必須</span>
200
- </div>
201
- <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
202
- </div>
203
- <div class="form-group">
204
- <div class='form-text-wrap'>
205
- <label class="form-text">番地</label>
206
- <span class="indispensable">必須</span>
207
- </div>
208
- <%= f.text_field :house_number, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
209
- </div>
210
- <div class="form-group">
211
- <div class='form-text-wrap'>
212
- <label class="form-text">建物名</label>
213
- <span class="form-any">任意</span>
214
- </div>
215
- <%= f.text_field :building_name, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
216
- </div>
217
- <div class="form-group">
218
- <div class='form-text-wrap'>
219
- <label class="form-text">電話番号</label>
220
- <span class="indispensable">必須</span>
221
- </div>
222
- <%= f.text_field :phone_number, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
223
- </div>
224
- </div>
225
- <%# /配送先の入力 %>
226
- <div class='buy-btn'>
227
- <%= f.submit "購入" ,class:"buy-red-btn" %>
228
- </div>
229
- <% end %>
230
- </div>
231
- </div>
232
- <%= render "shared/second-footer"%>
233
124
  ```
234
125
 
235
126
  試したこと
236
127
 
237
128
  デバッック(binding.pry)を利用して保存する情報がパラメーターに存在しているのかどうか見てみましたが、ちゃんと記録されているみたいでした。
238
- 条件分技のifの処理がtrueにならず、うまく行われていないのが原因みたいです。
129
+ 条件分技のifの処理がtrueにならず、うまく行われていないのが原因みたいです。
130
+ optinal:trueを購入履歴のモデルファイルにつけくわてみました。

4

userとitemのバリデーションを組んでみました。

2020/12/12 05:17

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -57,9 +57,12 @@
57
57
  validates :city
58
58
  validates :house_number
59
59
  validates :phone_number, numericality: { only_integer: true, message: "Input only number" }
60
+ validates :user
61
+ validates :item
60
62
  end
61
63
  validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9999999, message: "Out of setting range"}
62
64
  validates :price, numericality: { only_integer: true, message: "Half-width number." }
65
+
63
66
 
64
67
  with_options numericality: { other_than: 0, message: "Select" } do
65
68
  validates :item_condition_id

3

エラー文は出なくなりました。

2020/12/12 04:20

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,6 @@
1
1
  購入機能の実装のところで、購入情報をDBに保存したいのですが、そのままリダイレクトせずトップページに遷移せず、そのページに残ってしまいます。User must exist, Item must existtoiuというエラーメッセージがブラウザ上に表示されるだけの状態になってしまいます。
2
2
  どなたかご教授願えますでしょうか?よろしくお願いします。
3
-
3
+ ![イメージ説明](da1d90dd346a64558c8336350550f25e.png)
4
-
5
4
  ```ここに言語を入力
6
5
  app/controllers/purchase_history_controller.rb
7
6
 

2

エラー文は出なくなりました。

2020/12/11 16:18

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,6 @@
1
- 購入機能の実装のところで、購入情報をDBに保存したいのですが、以下エラー文が出す。エラはパスワードが定義されていないと出ているので各ファイルpasswordカラムを確認しましたが、どこも異常はありせんでした@user_itemの中身にpasswordが入らなから、エラーでストプしていと思うですが、どたかご教授願えますでょうか?よろしくお願ます。
1
+ 購入機能の実装のところで、購入情報をDBに保存したいのですが、のままリダイレクトせずトップペ遷移せずページに残ってしまUser must exist, Item must existtoiuとエラーセージがブラウザ上に表示されだけ状態にっています。
2
+ どなたかご教授願えますでしょうか?よろしくお願いします。
2
3
 
3
- ![イメージ説明](7e001212b021e1a637ce2c6a01d2872f.png)
4
- ![イメージ説明](39e4bc819dba5ae2008ddf782a320c53.png)
5
4
 
6
5
  ```ここに言語を入力
7
6
  app/controllers/purchase_history_controller.rb
@@ -28,7 +27,7 @@
28
27
  private
29
28
 
30
29
  def purchase_history_params
31
- 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)
30
+ 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)
32
31
  end
33
32
  end
34
33
 
@@ -234,4 +233,4 @@
234
233
  試したこと
235
234
 
236
235
  デバッック(binding.pry)を利用して保存する情報がパラメーターに存在しているのかどうか見てみましたが、ちゃんと記録されているみたいでした。
237
- @user_item.valid?を抜いてみましたが、今度はNameErrorが出力されてします。
236
+ 条件分技のifの処理trueにならずうまく行われていないのが原因みたいです。

1

住所情報と購入情報のインスタンスを作りました。

2020/12/11 16:14

投稿

murohi-08
murohi-08

スコア12

title CHANGED
File without changes
body CHANGED
@@ -13,12 +13,15 @@
13
13
  end
14
14
 
15
15
  def create
16
+ @item = Item.find(params[:item_id])
16
17
  @user_item = UserItem.new(purchase_history_params)
18
+ @user_item = SendingDestination.create
19
+ @user_item = PurchaseHistory.create
17
20
  if @user_item.valid?
18
21
  @user_item.save
19
- redirect_to action: :index
22
+ redirect_to root_path
20
23
  else
21
- render action: :new
24
+ render action: :index
22
25
  end
23
26
  end
24
27