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

質問編集履歴

6

修正

2020/10/01 08:53

投稿

kojiro23
kojiro23

スコア0

title CHANGED
File without changes
body CHANGED
@@ -11,144 +11,7 @@
11
11
 
12
12
  ### 該当のソースコード
13
13
 
14
- 購入画面
15
- ```
16
- <%= render "shared/second-header"%>
17
14
 
18
- <div class='transaction-contents'>
19
- <div class='transaction-main'>
20
- <h1 class='transaction-title-text'>
21
- 購入内容の確認
22
- </h1>
23
- <%# 購入内容の表示 %>
24
- <div class='buy-item-info'>
25
- <%= image_tag @item.image, class: 'buy-item-img' %>
26
- <div class='buy-item-right-content'>
27
- <h2 class='buy-item-text'>
28
- <%= @item.text %>
29
- </h2>
30
- <div class='buy-item-price'>
31
- <p class='item-price-text'>¥<%= @item.price %></p>
32
- <% if @item.delivery_fee.id == 1%>
33
- <p class='item-price-sub-text'>(税込)着払い</p>
34
- <% else %>
35
- <p class='item-price-sub-text'>(税込)送料込み</p>
36
- <% end %>
37
- </div>
38
- </div>
39
- </div>
40
- <%# /購入内容の表示 %>
41
-
42
- <%# 支払額の表示 %>
43
- <div class='item-payment'>
44
- <h1 class='item-payment-title'>
45
- 支払金額
46
- </h1>
47
- <p class='item-payment-price'>
48
- ¥<%= @item.price %>
49
- </p>
50
- </div>
51
- <%# /支払額の表示 %>
52
- <%= form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
53
- <%= render 'shared/error_messages', model: f.object %>
54
- <%# カード情報の入力 %>
55
- <div class='credit-card-form'>
56
- <h1 class='info-input-haedline'>
57
- クレジットカード情報入力
58
- </h1>
59
- <div class="form-group">
60
- <div class='form-text-wrap'>
61
- <label class="form-text">カード情報</label>
62
- <span class="indispensable">必須</span>
63
- </div>
64
- <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16", name:"number" %>
65
- <div class='available-card'>
66
- <%= image_tag 'card-visa.gif', class: 'card-logo'%>
67
- <%= image_tag 'card-mastercard.gif', class: 'card-logo'%>
68
- <%= image_tag 'card-jcb.gif', class: 'card-logo'%>
69
- <%= image_tag 'card-amex.gif', class: 'card-logo'%>
70
- </div>
71
- </div>
72
- <div class="form-group">
73
- <div class='form-text-wrap'>
74
- <label class="form-text">有効期限</label>
75
- <span class="indispensable">必須</span>
76
- </div>
77
- <div class='input-expiration-date-wrap'>
78
- <%= f.text_area :exe_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3", name:"exp_month" %>
79
- <p>月</p>
80
- <%= f.text_area :exe_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23", name:"exp_year" %>
81
- <p>年</p>
82
- </div>
83
- </div>
84
- <div class="form-group">
85
- <div class='form-text-wrap'>
86
- <label class="form-text">セキュリティコード</label>
87
- <span class="indispensable">必須</span>
88
- </div>
89
- <%= f.text_field :cvc,class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4", name:"cvc" %>
90
- </div>
91
- </div>
92
- <%# /カード情報の入力 %>
93
-
94
- <%# 配送先の入力 %>
95
- <div class='shipping-address-form'>
96
- <h1 class='info-input-haedline'>
97
- 配送先入力
98
- </h1>
99
- <div class="form-group">
100
- <div class='form-text-wrap'>
101
- <label class="form-text">郵便番号</label>
102
- <span class="indispensable">必須</span>
103
- </div>
104
- <%= f.text_field :postcode, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
105
- </div>
106
- <div class="form-group">
107
- <div class='form-text-wrap'>
108
- <label class="form-text">都道府県</label>
109
- <span class="indispensable">必須</span>
110
- </div>
111
- <%= f.collection_select(:area_id, Area.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
112
- </div>
113
- <div class="form-group">
114
- <div class='form-text-wrap'>
115
- <label class="form-text">市区町村</label>
116
- <span class="indispensable">必須</span>
117
- </div>
118
- <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
119
- </div>
120
- <div class="form-group">
121
- <div class='form-text-wrap'>
122
- <label class="form-text">番地</label>
123
- <span class="indispensable">必須</span>
124
- </div>
125
- <%= f.text_field :road, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
126
- </div>
127
- <div class="form-group">
128
- <div class='form-text-wrap'>
129
- <label class="form-text">建物名</label>
130
- <span class="form-any">任意</span>
131
- </div>
132
- <%= f.text_field :building, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
133
- </div>
134
- <div class="form-group">
135
- <div class='form-text-wrap'>
136
- <label class="form-text">電話番号</label>
137
- <span class="indispensable">必須</span>
138
- </div>
139
- <%= f.text_field :phone, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
140
- </div>
141
- </div>
142
- <%# /配送先の入力 %>
143
- <div class='buy-btn'>
144
- <%= f.submit "購入" ,class:"buy-red-btn" %>
145
- </div>
146
- <% end %>
147
- </div>
148
- </div>
149
- <%= render "shared/second-footer"%>
150
- ```
151
-
152
15
  purchases_controller.rb
153
16
  ```ruby
154
17
  class PurchasesController < ApplicationController
@@ -181,10 +44,11 @@
181
44
  #end
182
45
 
183
46
  def purchase_params
184
- params.require(:user_purchase).permit(:postcode, :area_id, :city, :road, :building, :phone).merge( user_id:      current_user.id, token: params[:token] )
47
+ params.require(:user_purchase).permit(:postcode, :area_id, :city, :road, :building, :phone).merge( user_id:      current_user.id, token: params[:token], item_id: params[:item_id] )
185
48
  end
186
49
 
187
50
  def pay_item
51
+
188
52
  Payjp.api_key = ENV["PAYJP_SECRET_KEY"]
189
53
  Payjp::Charge.create(
190
54
  amount: @item.price,
@@ -212,71 +76,14 @@
212
76
  end
213
77
 
214
78
  def save
215
- Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone)
216
79
  Purchase.create(user_id: user_id, item_id: item_id)
80
+ Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone, purchase_id: purchase.id)
217
81
  end
218
82
 
219
83
  end
220
84
  ```
221
- card.js
222
- ```javascript
223
- const pay = () => {
224
- Payjp.setPublicKey(process.env.PAYJP_PUBLIC_KEY);
225
- const form = document.getElementById("charge-form");
226
- form.addEventListener("submit", (e) => {
227
- e.preventDefault();
228
85
 
229
- const formResult = document.getElementById("charge-form");
230
- const formData = new FormData(formResult);
231
86
 
232
- const card = {
233
- number: formData.get("number"),
234
- cvc: formData.get("cvc"),
235
- exp_month: formData.get("exp_month"),
236
- exp_year: `20${formData.get("exp_year")}`,
237
- };
238
-
239
- Payjp.createToken(card, (status, response) => {
240
- if (status == 200) {
241
- const token = response.id;
242
- const renderDom = document.getElementById("charge-form");
243
- const tokenobj = `<input value=${token} name='token' type="hidden">`;
244
- renderDom.insertAdjacentHTML("beforeend", tokenobj);
245
- }
246
-
247
- document.getElementById("card-number").removeAttribute("name");
248
- document.getElementById("card-cvc").removeAttribute("name");
249
- document.getElementById("card-exp-month").removeAttribute("name");
250
- document.getElementById("card-exp-year").removeAttribute("name");
251
-
252
- document.getElementById("charge-form").submit();
253
- });
254
- });
255
- };
256
-
257
- window.addEventListener("load", pay);
258
- ```
259
- purchase.rb
260
- ```ruby
261
- class Purchase < ApplicationRecord
262
- attr_accessor :token
263
- has_one :address
264
- belongs_to :item
265
- belongs_to :user
266
- end
267
-
268
- ```
269
- address.rb
270
- ```ruby
271
- class Address < ApplicationRecord
272
- extend ActiveHash::Associations::ActiveRecordExtensions
273
- belongs_to_active_hash :area
274
-
275
- belongs_to :purchase
276
- end
277
-
278
- ```
279
-
280
87
  ### 試したこと
281
88
 
282
89
  itemの情報をまずは持ってくる記述をしたらいいと言われ、カリキュラムを確認、調べてみましたが分かりませんでした。

5

修正

2020/10/01 08:53

投稿

kojiro23
kojiro23

スコア0

title CHANGED
File without changes
body CHANGED
@@ -49,9 +49,6 @@
49
49
  </p>
50
50
  </div>
51
51
  <%# /支払額の表示 %>
52
- <%=# form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
53
- <%# <%= render 'shared/error_messages', model: @user_purchase %>
54
- <%=# 下記のように変更 %>
55
52
  <%= form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
56
53
  <%= render 'shared/error_messages', model: f.object %>
57
54
  <%# カード情報の入力 %>
@@ -158,7 +155,6 @@
158
155
 
159
156
  def index
160
157
  @item = Item.find(params[:item_id])
161
- <#% 追記 %>
162
158
  @user_purchase = UserPurchase.new
163
159
  end
164
160
 
@@ -180,8 +176,12 @@
180
176
 
181
177
  private
182
178
 
179
+ #def purchase_params
180
+ #params.permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id)
181
+ #end
182
+
183
- def purchase_params
183
+ def purchase_params
184
- params.permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id)
184
+ params.require(:user_purchase).permit(:postcode, :area_id, :city, :road, :building, :phone).merge( user_id:      current_user.id, token: params[:token] )
185
185
  end
186
186
 
187
187
  def pay_item
@@ -285,47 +285,24 @@
285
285
 
286
286
 
287
287
  追記
288
- user_purchase.rbの
289
288
 
290
- ```ruby
289
+ 修正内容
291
- def save
292
- Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone)
290
+ エラーメッセージはそのまま部分テンプレートを用いて表示することができました。
293
- Purchase.create(user_id: user_id, item_id: item_id)
291
+ コントローラーを修正するとフォームに入力した値の取得にも成功しました。(コードに修正箇所のせてます)
294
- end
295
- ```
296
- を下記に変更
297
- ```ruby
298
- def save
299
- address = Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone)
300
- end
301
- ```
302
292
 
293
+ 現在の状況ですが、フォームを入力して購入ボタンを押してもエラーメッセージは表示されません。
303
- purchases_controller.rbの
294
+ ですがテーブルに値を保存できずindexへ遷移されます。
304
295
 
305
- ```ruby
306
- def purchase_params
296
+ また別テーブルのitem情報を取得したいのですが、うまくいっておりません。
307
- params.permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id)
297
+ 私が考えているのがuser_purchase.rb内のsaveメソッド内に記述すれば取得できるのではと考えているのですが、うまくいっておりません。
308
- end
309
- ```
310
298
 
311
- 下記に変更
299
+ 現在方法調べている段階です。
300
+ 何かいい方法はありますでしょうか?
312
301
 
313
- ```ruby
314
- def purchase_params
315
- params.permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id, token: params[:token])
316
- end
317
- ```
318
302
 
319
- したところ以下のデータを取得できました。
320
303
 
321
- ![ターミナル](8c1cc49c537a05e30f9fa768a367d580.png)
322
304
 
323
- あと一歩のところで詰まっています。
324
- よろしくお願いいたします。
325
305
 
326
-
327
-
328
-
329
306
  ### 補足情報(FW/ツールのバージョンなど)
330
307
  Ruby 2.6.5
331
308
  Ruby on Rails 6

4

修正

2020/10/01 08:14

投稿

kojiro23
kojiro23

スコア0

title CHANGED
File without changes
body CHANGED
@@ -49,8 +49,11 @@
49
49
  </p>
50
50
  </div>
51
51
  <%# /支払額の表示 %>
52
- <%= form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
52
+ <%=# form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
53
53
  <%# <%= render 'shared/error_messages', model: @user_purchase %>
54
+ <%=# 下記のように変更 %>
55
+ <%= form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
56
+ <%= render 'shared/error_messages', model: f.object %>
54
57
  <%# カード情報の入力 %>
55
58
  <div class='credit-card-form'>
56
59
  <h1 class='info-input-haedline'>
@@ -155,6 +158,8 @@
155
158
 
156
159
  def index
157
160
  @item = Item.find(params[:item_id])
161
+ <#% 追記 %>
162
+ @user_purchase = UserPurchase.new
158
163
  end
159
164
 
160
165
  def new

3

書式の修正

2020/09/30 09:31

投稿

kojiro23
kojiro23

スコア0

title CHANGED
File without changes
body CHANGED
@@ -292,8 +292,6 @@
292
292
  ```ruby
293
293
  def save
294
294
  address = Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone)
295
- purchase = Purchase.create()
296
- UserPurchase.create(address_id: address.id, )
297
295
  end
298
296
  ```
299
297
 
@@ -309,7 +307,7 @@
309
307
 
310
308
  ```ruby
311
309
  def purchase_params
312
- params.require(:user_purchase).permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id, token: params[:token])
310
+ params.permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id, token: params[:token])
313
311
  end
314
312
  ```
315
313
 

2

書式の改善

2020/09/30 07:35

投稿

kojiro23
kojiro23

スコア0

title CHANGED
File without changes
body CHANGED
@@ -278,6 +278,51 @@
278
278
  他に必要な情報があればご指摘お願いします。
279
279
  ご教授よろしくお願いいたします。
280
280
 
281
+
282
+ 追記
283
+ user_purchase.rbの
284
+
285
+ ```ruby
286
+ def save
287
+ Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone)
288
+ Purchase.create(user_id: user_id, item_id: item_id)
289
+ end
290
+ ```
291
+ を下記に変更
292
+ ```ruby
293
+ def save
294
+ address = Address.create(postcode: postcode, area_id: area_id, city: city, road: road, building: building, phone: phone)
295
+ purchase = Purchase.create()
296
+ UserPurchase.create(address_id: address.id, )
297
+ end
298
+ ```
299
+
300
+ purchases_controller.rbの
301
+
302
+ ```ruby
303
+ def purchase_params
304
+ params.permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id)
305
+ end
306
+ ```
307
+
308
+ を下記に変更
309
+
310
+ ```ruby
311
+ def purchase_params
312
+ params.require(:user_purchase).permit(:postcode, :area_id, :city, :road, :building, :phone, :token).merge( user_id: current_user.id, token: params[:token])
313
+ end
314
+ ```
315
+
316
+ したところ以下のデータを取得できました。
317
+
318
+ ![ターミナル](8c1cc49c537a05e30f9fa768a367d580.png)
319
+
320
+ あと一歩のところで詰まっています。
321
+ よろしくお願いいたします。
322
+
323
+
324
+
325
+
281
326
  ### 補足情報(FW/ツールのバージョンなど)
282
327
  Ruby 2.6.5
283
328
  Ruby on Rails 6

1

商品購入画面を追加しました

2020/09/30 07:27

投稿

kojiro23
kojiro23

スコア0

title CHANGED
File without changes
body CHANGED
@@ -11,6 +11,144 @@
11
11
 
12
12
  ### 該当のソースコード
13
13
 
14
+ 購入画面
15
+ ```
16
+ <%= render "shared/second-header"%>
17
+
18
+ <div class='transaction-contents'>
19
+ <div class='transaction-main'>
20
+ <h1 class='transaction-title-text'>
21
+ 購入内容の確認
22
+ </h1>
23
+ <%# 購入内容の表示 %>
24
+ <div class='buy-item-info'>
25
+ <%= image_tag @item.image, class: 'buy-item-img' %>
26
+ <div class='buy-item-right-content'>
27
+ <h2 class='buy-item-text'>
28
+ <%= @item.text %>
29
+ </h2>
30
+ <div class='buy-item-price'>
31
+ <p class='item-price-text'>¥<%= @item.price %></p>
32
+ <% if @item.delivery_fee.id == 1%>
33
+ <p class='item-price-sub-text'>(税込)着払い</p>
34
+ <% else %>
35
+ <p class='item-price-sub-text'>(税込)送料込み</p>
36
+ <% end %>
37
+ </div>
38
+ </div>
39
+ </div>
40
+ <%# /購入内容の表示 %>
41
+
42
+ <%# 支払額の表示 %>
43
+ <div class='item-payment'>
44
+ <h1 class='item-payment-title'>
45
+ 支払金額
46
+ </h1>
47
+ <p class='item-payment-price'>
48
+ ¥<%= @item.price %>
49
+ </p>
50
+ </div>
51
+ <%# /支払額の表示 %>
52
+ <%= form_with model: @user_purchase, url: item_purchases_path, id: 'charge-form', local: true do |f| %>
53
+ <%# <%= render 'shared/error_messages', model: @user_purchase %>
54
+ <%# カード情報の入力 %>
55
+ <div class='credit-card-form'>
56
+ <h1 class='info-input-haedline'>
57
+ クレジットカード情報入力
58
+ </h1>
59
+ <div class="form-group">
60
+ <div class='form-text-wrap'>
61
+ <label class="form-text">カード情報</label>
62
+ <span class="indispensable">必須</span>
63
+ </div>
64
+ <%= f.text_field :number, class:"input-default", id:"card-number", placeholder:"カード番号(半角英数字)", maxlength:"16", name:"number" %>
65
+ <div class='available-card'>
66
+ <%= image_tag 'card-visa.gif', class: 'card-logo'%>
67
+ <%= image_tag 'card-mastercard.gif', class: 'card-logo'%>
68
+ <%= image_tag 'card-jcb.gif', class: 'card-logo'%>
69
+ <%= image_tag 'card-amex.gif', class: 'card-logo'%>
70
+ </div>
71
+ </div>
72
+ <div class="form-group">
73
+ <div class='form-text-wrap'>
74
+ <label class="form-text">有効期限</label>
75
+ <span class="indispensable">必須</span>
76
+ </div>
77
+ <div class='input-expiration-date-wrap'>
78
+ <%= f.text_area :exe_month, class:"input-expiration-date", id:"card-exp-month", placeholder:"例)3", name:"exp_month" %>
79
+ <p>月</p>
80
+ <%= f.text_area :exe_year, class:"input-expiration-date", id:"card-exp-year", placeholder:"例)23", name:"exp_year" %>
81
+ <p>年</p>
82
+ </div>
83
+ </div>
84
+ <div class="form-group">
85
+ <div class='form-text-wrap'>
86
+ <label class="form-text">セキュリティコード</label>
87
+ <span class="indispensable">必須</span>
88
+ </div>
89
+ <%= f.text_field :cvc,class:"input-default", id:"card-cvc", placeholder:"カード背面4桁もしくは3桁の番号", maxlength:"4", name:"cvc" %>
90
+ </div>
91
+ </div>
92
+ <%# /カード情報の入力 %>
93
+
94
+ <%# 配送先の入力 %>
95
+ <div class='shipping-address-form'>
96
+ <h1 class='info-input-haedline'>
97
+ 配送先入力
98
+ </h1>
99
+ <div class="form-group">
100
+ <div class='form-text-wrap'>
101
+ <label class="form-text">郵便番号</label>
102
+ <span class="indispensable">必須</span>
103
+ </div>
104
+ <%= f.text_field :postcode, class:"input-default", id:"postal-code", placeholder:"例)123-4567", maxlength:"8" %>
105
+ </div>
106
+ <div class="form-group">
107
+ <div class='form-text-wrap'>
108
+ <label class="form-text">都道府県</label>
109
+ <span class="indispensable">必須</span>
110
+ </div>
111
+ <%= f.collection_select(:area_id, Area.all, :id, :name, {}, {class:"select-box", id:"prefecture"}) %>
112
+ </div>
113
+ <div class="form-group">
114
+ <div class='form-text-wrap'>
115
+ <label class="form-text">市区町村</label>
116
+ <span class="indispensable">必須</span>
117
+ </div>
118
+ <%= f.text_field :city, class:"input-default", id:"city", placeholder:"例)横浜市緑区"%>
119
+ </div>
120
+ <div class="form-group">
121
+ <div class='form-text-wrap'>
122
+ <label class="form-text">番地</label>
123
+ <span class="indispensable">必須</span>
124
+ </div>
125
+ <%= f.text_field :road, class:"input-default", id:"addresses", placeholder:"例)青山1-1-1"%>
126
+ </div>
127
+ <div class="form-group">
128
+ <div class='form-text-wrap'>
129
+ <label class="form-text">建物名</label>
130
+ <span class="form-any">任意</span>
131
+ </div>
132
+ <%= f.text_field :building, class:"input-default", id:"building", placeholder:"例)柳ビル103"%>
133
+ </div>
134
+ <div class="form-group">
135
+ <div class='form-text-wrap'>
136
+ <label class="form-text">電話番号</label>
137
+ <span class="indispensable">必須</span>
138
+ </div>
139
+ <%= f.text_field :phone, class:"input-default", id:"phone-number", placeholder:"例)09012345678",maxlength:"11"%>
140
+ </div>
141
+ </div>
142
+ <%# /配送先の入力 %>
143
+ <div class='buy-btn'>
144
+ <%= f.submit "購入" ,class:"buy-red-btn" %>
145
+ </div>
146
+ <% end %>
147
+ </div>
148
+ </div>
149
+ <%= render "shared/second-footer"%>
150
+ ```
151
+
14
152
  purchases_controller.rb
15
153
  ```ruby
16
154
  class PurchasesController < ApplicationController