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

質問編集履歴

3

情報を修正しました。

2018/01/13 13:36

投稿

begenner
begenner

スコア80

title CHANGED
File without changes
body CHANGED
@@ -3,18 +3,17 @@
3
3
  サンプルで作成したRailsアプリケーションではうまくいっていましたが、新しく作成して同じような動作をしたいのですのですが、javascriptでの検索がうまくいきません。
4
4
  できるだけコードを変更せずにやっていきたいと思っています。
5
5
  わかる方がいらっしゃればよろしくお願いします。
6
- ###エラー詳細(terminalのログ画像![イメージ説明](c6721c8e4a072b0423cee241f59b46d5.png))
6
+ ###エラー詳細(terminalのログ画像)![イメージ説明](c6721c8e4a072b0423cee241f59b46d5.png)
7
-
7
+ ![イメージ説明](485422b781a1e3e66a336d243d8f25b2.png)
8
8
  ###期待する動作
9
9
  - データベースにコード番号をpostして情報を取得、取得した値をフォームのテキストボックスに表示する
10
10
  ###問題点
11
11
  - データベースにコード番号をpostして情報をテキストボックスに表示できない(モデル検索がうまくいっていないことが原因だと思います)
12
12
  ###実現したいこと
13
13
  コード番号を入力するとそれに基づいたデータを参照して表示させたい。
14
- ###エラー内容
15
- 下記のエラーが表示されます
16
- ![イメージ説明](485422b781a1e3e66a336d243d8f25b2.png)
17
14
 
15
+
16
+
18
17
  ###サンプルアプリケーション作成で参考にしたサイト
19
18
  [Railsでajaxを用いた簡単なインクリメンタルサーチを実装する](https://qiita.com/yuki-n/items/fdc5f7d5ac2f128221d1)
20
19
 

2

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

2018/01/13 13:36

投稿

begenner
begenner

スコア80

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,12 @@
3
3
  サンプルで作成したRailsアプリケーションではうまくいっていましたが、新しく作成して同じような動作をしたいのですのですが、javascriptでの検索がうまくいきません。
4
4
  できるだけコードを変更せずにやっていきたいと思っています。
5
5
  わかる方がいらっしゃればよろしくお願いします。
6
+ ###エラー詳細(terminalのログ画像![イメージ説明](c6721c8e4a072b0423cee241f59b46d5.png))
6
7
 
8
+ ###期待する動作
9
+ - データベースにコード番号をpostして情報を取得、取得した値をフォームのテキストボックスに表示する
10
+ ###問題点
11
+ - データベースにコード番号をpostして情報をテキストボックスに表示できない(モデル検索がうまくいっていないことが原因だと思います)
7
12
  ###実現したいこと
8
13
  コード番号を入力するとそれに基づいたデータを参照して表示させたい。
9
14
  ###エラー内容
@@ -23,149 +28,6 @@
23
28
  - 商品モデルの名前を変更(ユーザーレベルごとに商品単価が違うため複数作成するため)
24
29
  - サンプルの商品モデルは product、新規アプリケーションは product_rank0 となっている
25
30
  - 商品モデルのカラム名が変更されている(基本的に内容は同じ)
26
- ###うまくいっていたコード(必要だと思われる内容だけ)
27
-
28
- データベース登録内容(商品モデル、少し余分な内容も含まれています)
29
- ![イメージ説明](757ebca5a0d8c6ad36b0b6784c8e10d8.png)
30
- schema.rb(データベース)
31
- ```ruby
32
- ActiveRecord::Schema.define(version: 20171217113147) do
33
-
34
- #注文テーブル
35
- create_table "orders", force: :cascade do |t|
36
- t.string "order_no", limit: 255
37
- t.integer "user_id", limit: 4
38
- t.datetime "created_at", null: false
39
- t.datetime "updated_at", null: false
40
- end
41
-
42
- #注文明細テーブル
43
- create_table "orderdetails", force: :cascade do |t|
44
- t.integer "code_no", limit: 4 #コード番号
45
- t.integer "quantity", limit: 4
46
- t.float "price", limit: 24 #価格
47
- t.string "product_name", limit: 255 #商品名
48
- t.string "unit", limit: 255 #単位
49
- t.float "subtotal", limit: 24 #小計
50
- end
51
-
52
- #商品テーブル
53
- create_table "products", force: :cascade do |t|
54
- t.integer "code_no", limit: 4, null: false #コード番号
55
- t.string "product_name", limit: 255, null: false#商品名
56
- t.integer "selling_price1", limit: 4, null: false#販売価格
57
- t.string "unit", limit: 255#単位
58
- end
59
-
60
- end
61
-
62
- ```
63
-
64
-
65
- config/routes.rb
66
- ```ruby
67
- Rails.application.routes.draw do
68
-
69
- resources :orders do
70
- collection do
71
- get 'search_product'
72
- end
73
- end
74
- resources :orderdetails
75
- resources :products
76
- root to: 'top#index'
77
- ```
78
- app/controllers/orders_controllers.rb
79
- ```ruby
80
- class OrdersController < ApplicationController
81
- before_action :set_order, only: [:show, :edit, :update, :destroy]
82
- .
83
- .
84
-
85
- # GET /orders/new
86
- def new
87
- @order = Order.new
88
-
89
- (1..10).each do |num|
90
- @order.orderdetails.build
91
- end
92
-
93
- end
94
- .
95
- .
96
-
97
- # jsonでデータを取得する
98
- def search_product
99
- @product = Product.where('code_no LIKE(?)', "#{params[:keyword]}")
100
- render json: @product
101
- end
102
- .
103
- .
104
- end
105
- ```
106
- app/assets/javascript/orders.js
107
- ```javascript
108
- $(document).on('turbolinks:load', function(){
109
- $('#order_orderdetails_attributes_0_code_no').change(function () {
110
-
111
- var input = ($(this).val());
112
- $.ajax({
113
- url: '/orders/search_product',
114
- type: 'GET',
115
- data: ('keyword=' + input),
116
- processData: false,
117
- contentType: false,
118
- dataType: 'json'
119
- })
120
- .done(function(data){
121
- $(data).each(function(i, product){
122
- $("#order_orderdetails_attributes_0_product_name").val(product.product_name)
123
- $("#order_orderdetails_attributes_0_unit").val(product.unit)
124
- $("#order_orderdetails_attributes_0_price").val(product.selling_price1)
125
- });
126
- });
127
- });
128
-
129
- // 2番目の入力項目
130
- .
131
- .
132
- });
133
- ```
134
- app/views/orders/new.html.erb
135
- ```ruby
136
- <%= form_for (@order), html:{id: 'form'} do |f|%>
137
- 注文内容
138
- <table border="1">
139
- <thead>
140
- <tr>
141
- <th>コード</th>
142
- <th>品名</th>
143
- <th style="width: 78px;">数量</th>
144
- <th>価格</th>
145
- <th>金額</th>
146
- </tr>
147
- </thead>
148
- <tbody>
149
- <%= f.fields_for :orderdetails do |orderdetail, s | %>
150
-
151
- <tr>
152
- <td><%= orderdetail.text_field 'code_no', :size => "5"%></td>
153
- <td><%= orderdetail.text_field 'product_name', disabled: 'disabled' %></td>
154
- <td><%= orderdetail.text_field 'quantity', :size => "3" %>&times;<%= orderdetail.text_field 'unit', :size => "1", disabled: 'disabled' %></td>
155
- <td><%= orderdetail.text_field 'price', :size => "7", disabled: 'disabled' %></td>
156
- <td><%= orderdetail.text_field 'subtotal', :size => "7", disabled: 'disabled' %></td>
157
- </tr>
158
- <% end %>
159
- <%= f.hidden_field :user_id, value: current_user.id %>
160
- </tbody>
161
- </table>
162
- <br />
163
- <div class="actions">
164
- <%= f.submit %>
165
- </div>
166
- <% end %>
167
- ```
168
-
169
31
  ###うまくいっていないコード(必要だと思われる内容だけ)
170
32
  データベース登録内容(商品モデル、少し余分な内容も含まれています(エラー原因の可能性要素1))
171
33
  ![イメージ説明](197fb2f15bd89cb101f2dc52e6483210.png)
@@ -269,20 +131,8 @@
269
131
  app/views/orders/new.html.erb
270
132
  ```ruby
271
133
  <%= form_for (@order), html:{id: 'form'} do |f|%>
272
- 注文内容
134
+
273
- <table border="1">
274
- <thead>
275
- <tr>
276
- <th>コード</th>
277
- <th>品名</th>
278
- <th style="width: 78px;">数量</th>
279
- <th>価格</th>
280
- <th>金額</th>
281
- </tr>
282
- </thead>
283
- <tbody>
284
135
  <%= f.fields_for :orderdetails do |orderdetail, s | %>
285
-
286
136
  <tr>
287
137
  <td><%= orderdetail.text_field 'code_no', :size => "5"%></td>
288
138
  <td><%= orderdetail.text_field 'product_name', disabled: 'disabled' %></td>
@@ -292,7 +142,6 @@
292
142
  </tr>
293
143
  <% end %>
294
144
  <%= f.hidden_field :user_id, value: current_user.id %>
295
- </tbody>
296
145
  </table>
297
146
  <br />
298
147
  <div class="actions">

1

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

2018/01/13 13:32

投稿

begenner
begenner

スコア80

title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,9 @@
10
10
  下記のエラーが表示されます
11
11
  ![イメージ説明](485422b781a1e3e66a336d243d8f25b2.png)
12
12
 
13
+ ###サンプルアプリケーション作成で参考にしたサイト
14
+ [Railsでajaxを用いた簡単なインクリメンタルサーチを実装する](https://qiita.com/yuki-n/items/fdc5f7d5ac2f128221d1)
15
+
13
16
  ###動作環境
14
17
  OS:mac
15
18
  Ruby:2.3.5