質問編集履歴

3

情報を修正しました。

2018/01/13 13:36

投稿

begenner
begenner

スコア79

test CHANGED
File without changes
test CHANGED
@@ -8,9 +8,9 @@
8
8
 
9
9
  わかる方がいらっしゃればよろしくお願いします。
10
10
 
11
- ###エラー詳細(terminalのログ画像![イメージ説明](c6721c8e4a072b0423cee241f59b46d5.png))
11
+ ###エラー詳細(terminalのログ画像)![イメージ説明](c6721c8e4a072b0423cee241f59b46d5.png)
12
+
12
-
13
+ ![イメージ説明](485422b781a1e3e66a336d243d8f25b2.png)
13
-
14
14
 
15
15
  ###期待する動作
16
16
 
@@ -24,11 +24,9 @@
24
24
 
25
25
  コード番号を入力するとそれに基づいたデータを参照して表示させたい。
26
26
 
27
- ###エラー内容
27
+
28
-
29
- 下記のエラーが表示されます
28
+
30
-
31
- ![イメージ説明](485422b781a1e3e66a336d243d8f25b2.png)
29
+
32
30
 
33
31
 
34
32
 

2

コードをうまくいかなかったコードだけとし、terminalのログ画像を追加いたしました。

2018/01/13 13:36

投稿

begenner
begenner

スコア79

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,17 @@
8
8
 
9
9
  わかる方がいらっしゃればよろしくお願いします。
10
10
 
11
-
11
+ ###エラー詳細(terminalのログ画像![イメージ説明](c6721c8e4a072b0423cee241f59b46d5.png))
12
+
13
+
14
+
15
+ ###期待する動作
16
+
17
+ - データベースにコード番号をpostして情報を取得、取得した値をフォームのテキストボックスに表示する
18
+
19
+ ###問題点
20
+
21
+ - データベースにコード番号をpostして情報をテキストボックスに表示できない(モデル検索がうまくいっていないことが原因だと思います)
12
22
 
13
23
  ###実現したいこと
14
24
 
@@ -48,19 +58,39 @@
48
58
 
49
59
  - 商品モデルのカラム名が変更されている(基本的に内容は同じ)
50
60
 
51
- ###うまくいっていコード(必要だと思われる内容だけ)
61
+ ###うまくいっていないコード(必要だと思われる内容だけ)
52
-
53
-
54
-
62
+
55
- データベース登録内容(商品モデル、少し余分な内容も含まれています)
63
+ データベース登録内容(商品モデル、少し余分な内容も含まれています(エラー原因の可能性要素1))
56
-
64
+
57
- ![イメージ説明](757ebca5a0d8c6ad36b0b6784c8e10d8.png)
65
+ ![イメージ説明](197fb2f15bd89cb101f2dc52e6483210.png)
58
-
66
+
67
+
68
+
59
- schema.rb(データベース)
69
+ schema.rb
60
70
 
61
71
  ```ruby
62
72
 
63
- ActiveRecord::Schema.define(version: 20171217113147) do
73
+ ActiveRecord::Schema.define(version: 20180112101528) do
74
+
75
+ #注文明細テーブル
76
+
77
+ create_table "orderdetails", force: :cascade do |t|
78
+
79
+ t.integer "order_id", limit: 4 #注文テーブルのid
80
+
81
+ t.integer "code_no", limit: 4 #コード番号
82
+
83
+ t.string "product_name", limit: 255#商品名
84
+
85
+ t.integer "quantity", limit: 4 #数量
86
+
87
+ t.float "price", limit: 24 #価格
88
+
89
+ t.string "unit", limit: 255 #単位
90
+
91
+ t.integer "sub_total", limit: 4 #小計
92
+
93
+ end
64
94
 
65
95
 
66
96
 
@@ -80,224 +110,164 @@
80
110
 
81
111
 
82
112
 
83
- #注文明細テーブル
84
-
85
- create_table "orderdetails", force: :cascade do |t|
86
-
87
- t.integer "code_no", limit: 4 #コード番号
88
-
89
- t.integer "quantity", limit: 4
90
-
91
- t.float "price", limit: 24 #価格
92
-
93
- t.string "product_name", limit: 255 #商品名
94
-
95
- t.string "unit", limit: 255 #単位
96
-
97
- t.float "subtotal", limit: 24 #小計
98
-
99
- end
100
-
101
-
102
-
103
113
  #商品テーブル
104
114
 
105
- create_table "products", force: :cascade do |t|
115
+ create_table "product_rank0s", force: :cascade do |t|
106
-
116
+
107
- t.integer "code_no", limit: 4, null: false #コード番号
117
+ t.string "code_no", limit: 255 #コード番号
108
-
118
+
109
- t.string "product_name", limit: 255, null: false#商品名
119
+ t.string "product_name", limit: 255 #商品名
110
-
111
- t.integer "selling_price1", limit: 4, null: false#販売価格
120
+
112
-
113
- t.string "unit", limit: 255#単位
121
+ t.string "unit", limit: 255 #単位
122
+
114
-
123
+ t.float "s_price1", limit: 24 #販売価格
124
+
115
- end
125
+ end
126
+
116
-
127
+ ```
128
+
129
+
130
+
117
-
131
+ config/routes.rb
132
+
133
+ ```ruby
134
+
135
+ Rails.application.routes.draw do
136
+
137
+
138
+
139
+ resources :orders do
140
+
141
+ collection do
142
+
143
+ get 'search_product'
144
+
145
+ end
146
+
147
+ end
148
+
149
+
150
+
151
+ resources :orderdetails
152
+
153
+ root to: 'tops#index'
118
154
 
119
155
  end
120
156
 
121
-
122
-
123
- ```
157
+ ```
124
-
125
-
126
-
127
-
128
-
158
+
129
- config/routes.rb
159
+ app/controllers/orders_controllers.rb(エラー原因の可能性要素2)
130
160
 
131
161
  ```ruby
132
162
 
133
- Rails.application.routes.draw do
163
+ class OrdersController < ApplicationController
164
+
134
-
165
+ def new
135
-
136
-
166
+
137
- resources :orders do
167
+ @order = Order.new
138
-
139
- collection do
168
+
140
-
169
+
170
+
141
- get 'search_product'
171
+ (1..10).each do |num|
172
+
173
+ @order.orderdetails.build
142
174
 
143
175
  end
144
176
 
145
- end
177
+ end
178
+
146
-
179
+ .
180
+
181
+ .
182
+
183
+ # jsonでデータを取得する
184
+
147
- resources :orderdetails
185
+ def search_product
186
+
187
+
188
+
148
-
189
+ @product = Product_rank0.where('code_no LIKE(?)', "#{params[:keyword]}")
190
+
149
- resources :products
191
+ render json: @product
192
+
150
-
193
+      #試した内容1
194
+
195
+ # @product = Product_rank0.find_by(code_no: "#{params[:keyword]}")
196
+
151
- root to: 'top#index'
197
+ # render json: @product
198
+
152
-
199
+ end
200
+
201
+ .
202
+
203
+ .
204
+
205
+ end
206
+
153
- ```
207
+ ```
208
+
154
-
209
+ app/assets/javascript/orders.js(エラー原因の可能性要素3)
210
+
211
+ ```javascript
212
+
213
+ $(document).on('turbolinks:load', function(){
214
+
215
+ $('#order_orderdetails_attributes_0_code_no').change(function () {
216
+
217
+ var input = ($(this).val());
218
+
219
+ $.ajax({
220
+
221
+ url: '/orders/search_product',
222
+
223
+ type: 'GET',
224
+
225
+ data: ('keyword=' + input),
226
+
227
+ processData: false,
228
+
229
+ contentType: false,
230
+
231
+ dataType: 'json'
232
+
233
+ })
234
+
235
+ .done(function(data){
236
+
237
+ $(data).each(function(i, product){
238
+
239
+ $('#order_orderdetails_attributes_0_part_number').val(product.part_number)
240
+
241
+ $("#order_orderdetails_attributes_0_product_name").val(product.product_name)
242
+
243
+ $("#order_orderdetails_attributes_0_unit").val(product.unit) $("#order_orderdetails_attributes_0_price").val(product.selling_price1)
244
+
245
+ });
246
+
247
+ });
248
+
249
+ });
250
+
251
+ // 2番目の入力項目
252
+
253
+ .
254
+
255
+ .
256
+
257
+ });
258
+
259
+ ```
260
+
155
- app/controllers/orders_controllers.rb
261
+ app/views/orders/new.html.erb
156
262
 
157
263
  ```ruby
158
264
 
159
- class OrdersController < ApplicationController
160
-
161
- before_action :set_order, only: [:show, :edit, :update, :destroy]
162
-
163
- .
164
-
165
- .
166
-
167
-
168
-
169
- # GET /orders/new
170
-
171
- def new
172
-
173
- @order = Order.new
174
-
175
-
176
-
177
- (1..10).each do |num|
178
-
179
- @order.orderdetails.build
180
-
181
- end
182
-
183
-
184
-
185
- end
186
-
187
- .
188
-
189
- .
190
-
191
-
192
-
193
- # jsonでデータを取得する
194
-
195
- def search_product
196
-
197
- @product = Product.where('code_no LIKE(?)', "#{params[:keyword]}")
198
-
199
- render json: @product
200
-
201
- end
202
-
203
- .
204
-
205
- .
206
-
207
- end
208
-
209
- ```
210
-
211
- app/assets/javascript/orders.js
212
-
213
- ```javascript
214
-
215
- $(document).on('turbolinks:load', function(){
216
-
217
- $('#order_orderdetails_attributes_0_code_no').change(function () {
218
-
219
-
220
-
221
- var input = ($(this).val());
222
-
223
- $.ajax({
224
-
225
- url: '/orders/search_product',
226
-
227
- type: 'GET',
228
-
229
- data: ('keyword=' + input),
230
-
231
- processData: false,
232
-
233
- contentType: false,
234
-
235
- dataType: 'json'
236
-
237
- })
238
-
239
- .done(function(data){
240
-
241
- $(data).each(function(i, product){
242
-
243
- $("#order_orderdetails_attributes_0_product_name").val(product.product_name)
244
-
245
- $("#order_orderdetails_attributes_0_unit").val(product.unit)
246
-
247
- $("#order_orderdetails_attributes_0_price").val(product.selling_price1)
248
-
249
- });
250
-
251
- });
252
-
253
- });
254
-
255
-
256
-
257
- // 2番目の入力項目
258
-
259
- .
260
-
261
- .
262
-
263
- });
264
-
265
- ```
266
-
267
- app/views/orders/new.html.erb
268
-
269
- ```ruby
270
-
271
265
  <%= form_for (@order), html:{id: 'form'} do |f|%>
272
266
 
273
- 注文内容
267
+
274
-
275
- <table border="1">
276
-
277
- <thead>
278
-
279
- <tr>
280
-
281
- <th>コード</th>
282
-
283
- <th>品名</th>
284
-
285
- <th style="width: 78px;">数量</th>
286
-
287
- <th>価格</th>
288
-
289
- <th>金額</th>
290
-
291
- </tr>
292
-
293
- </thead>
294
-
295
- <tbody>
296
268
 
297
269
  <%= f.fields_for :orderdetails do |orderdetail, s | %>
298
270
 
299
-
300
-
301
271
  <tr>
302
272
 
303
273
  <td><%= orderdetail.text_field 'code_no', :size => "5"%></td>
@@ -316,8 +286,6 @@
316
286
 
317
287
  <%= f.hidden_field :user_id, value: current_user.id %>
318
288
 
319
- </tbody>
320
-
321
289
  </table>
322
290
 
323
291
  <br />
@@ -330,276 +298,6 @@
330
298
 
331
299
  <% end %>
332
300
 
301
+
302
+
333
- ```
303
+ ```
334
-
335
-
336
-
337
- ###うまくいっていないコード(必要だと思われる内容だけ)
338
-
339
- データベース登録内容(商品モデル、少し余分な内容も含まれています(エラー原因の可能性要素1))
340
-
341
- ![イメージ説明](197fb2f15bd89cb101f2dc52e6483210.png)
342
-
343
-
344
-
345
- schema.rb
346
-
347
- ```ruby
348
-
349
- ActiveRecord::Schema.define(version: 20180112101528) do
350
-
351
- #注文明細テーブル
352
-
353
- create_table "orderdetails", force: :cascade do |t|
354
-
355
- t.integer "order_id", limit: 4 #注文テーブルのid
356
-
357
- t.integer "code_no", limit: 4 #コード番号
358
-
359
- t.string "product_name", limit: 255#商品名
360
-
361
- t.integer "quantity", limit: 4 #数量
362
-
363
- t.float "price", limit: 24 #価格
364
-
365
- t.string "unit", limit: 255 #単位
366
-
367
- t.integer "sub_total", limit: 4 #小計
368
-
369
- end
370
-
371
-
372
-
373
- #注文テーブル
374
-
375
- create_table "orders", force: :cascade do |t|
376
-
377
- t.string "order_no", limit: 255
378
-
379
- t.integer "user_id", limit: 4
380
-
381
- t.datetime "created_at", null: false
382
-
383
- t.datetime "updated_at", null: false
384
-
385
- end
386
-
387
-
388
-
389
- #商品テーブル
390
-
391
- create_table "product_rank0s", force: :cascade do |t|
392
-
393
- t.string "code_no", limit: 255 #コード番号
394
-
395
- t.string "product_name", limit: 255 #商品名
396
-
397
- t.string "unit", limit: 255 #単位
398
-
399
- t.float "s_price1", limit: 24 #販売価格
400
-
401
- end
402
-
403
- ```
404
-
405
-
406
-
407
- config/routes.rb
408
-
409
- ```ruby
410
-
411
- Rails.application.routes.draw do
412
-
413
-
414
-
415
- resources :orders do
416
-
417
- collection do
418
-
419
- get 'search_product'
420
-
421
- end
422
-
423
- end
424
-
425
-
426
-
427
- resources :orderdetails
428
-
429
- root to: 'tops#index'
430
-
431
- end
432
-
433
- ```
434
-
435
- app/controllers/orders_controllers.rb(エラー原因の可能性要素2)
436
-
437
- ```ruby
438
-
439
- class OrdersController < ApplicationController
440
-
441
- def new
442
-
443
- @order = Order.new
444
-
445
-
446
-
447
- (1..10).each do |num|
448
-
449
- @order.orderdetails.build
450
-
451
- end
452
-
453
- end
454
-
455
- .
456
-
457
- .
458
-
459
- # jsonでデータを取得する
460
-
461
- def search_product
462
-
463
-
464
-
465
- @product = Product_rank0.where('code_no LIKE(?)', "#{params[:keyword]}")
466
-
467
- render json: @product
468
-
469
-      #試した内容1
470
-
471
- # @product = Product_rank0.find_by(code_no: "#{params[:keyword]}")
472
-
473
- # render json: @product
474
-
475
- end
476
-
477
- .
478
-
479
- .
480
-
481
- end
482
-
483
- ```
484
-
485
- app/assets/javascript/orders.js(エラー原因の可能性要素3)
486
-
487
- ```javascript
488
-
489
- $(document).on('turbolinks:load', function(){
490
-
491
- $('#order_orderdetails_attributes_0_code_no').change(function () {
492
-
493
- var input = ($(this).val());
494
-
495
- $.ajax({
496
-
497
- url: '/orders/search_product',
498
-
499
- type: 'GET',
500
-
501
- data: ('keyword=' + input),
502
-
503
- processData: false,
504
-
505
- contentType: false,
506
-
507
- dataType: 'json'
508
-
509
- })
510
-
511
- .done(function(data){
512
-
513
- $(data).each(function(i, product){
514
-
515
- $('#order_orderdetails_attributes_0_part_number').val(product.part_number)
516
-
517
- $("#order_orderdetails_attributes_0_product_name").val(product.product_name)
518
-
519
- $("#order_orderdetails_attributes_0_unit").val(product.unit) $("#order_orderdetails_attributes_0_price").val(product.selling_price1)
520
-
521
- });
522
-
523
- });
524
-
525
- });
526
-
527
- // 2番目の入力項目
528
-
529
- .
530
-
531
- .
532
-
533
- });
534
-
535
- ```
536
-
537
- app/views/orders/new.html.erb
538
-
539
- ```ruby
540
-
541
- <%= form_for (@order), html:{id: 'form'} do |f|%>
542
-
543
- 注文内容
544
-
545
- <table border="1">
546
-
547
- <thead>
548
-
549
- <tr>
550
-
551
- <th>コード</th>
552
-
553
- <th>品名</th>
554
-
555
- <th style="width: 78px;">数量</th>
556
-
557
- <th>価格</th>
558
-
559
- <th>金額</th>
560
-
561
- </tr>
562
-
563
- </thead>
564
-
565
- <tbody>
566
-
567
- <%= f.fields_for :orderdetails do |orderdetail, s | %>
568
-
569
-
570
-
571
- <tr>
572
-
573
- <td><%= orderdetail.text_field 'code_no', :size => "5"%></td>
574
-
575
- <td><%= orderdetail.text_field 'product_name', disabled: 'disabled' %></td>
576
-
577
- <td><%= orderdetail.text_field 'quantity', :size => "3" %>&times;<%= orderdetail.text_field 'unit', :size => "1", disabled: 'disabled' %></td>
578
-
579
- <td><%= orderdetail.text_field 'price', :size => "7", disabled: 'disabled' %></td>
580
-
581
- <td><%= orderdetail.text_field 'subtotal', :size => "7", disabled: 'disabled' %></td>
582
-
583
- </tr>
584
-
585
- <% end %>
586
-
587
- <%= f.hidden_field :user_id, value: current_user.id %>
588
-
589
- </tbody>
590
-
591
- </table>
592
-
593
- <br />
594
-
595
- <div class="actions">
596
-
597
- <%= f.submit %>
598
-
599
- </div>
600
-
601
- <% end %>
602
-
603
-
604
-
605
- ```

1

サンプルアプリケーション作成時の参考サイトのURLを追記しました。

2018/01/13 13:32

投稿

begenner
begenner

スコア79

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,12 @@
22
22
 
23
23
 
24
24
 
25
+ ###サンプルアプリケーション作成で参考にしたサイト
26
+
27
+ [Railsでajaxを用いた簡単なインクリメンタルサーチを実装する](https://qiita.com/yuki-n/items/fdc5f7d5ac2f128221d1)
28
+
29
+
30
+
25
31
  ###動作環境
26
32
 
27
33
  OS:mac