質問編集履歴

3

誤字を修正しました。

2017/12/16 12:48

投稿

begenner
begenner

スコア79

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  [ruby の eachメソッドでループ回数が必要な場合 (for文のiのような)](http://useless.hatenablog.com/entry/2016/07/11/190000)
62
62
 
63
- のサイトのように fields_forのyyyに対してブロックの中で s を参考に連番として当てられないかと試しましたができませんでした。
63
+ のサイトのように fields_forのyyyに対してブロックの中で s を連番として当てられないかと試しましたができませんでした。
64
64
 
65
65
 
66
66
 

2

コード、試したことを追記しました

2017/12/16 12:47

投稿

begenner
begenner

スコア79

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
 
6
6
 
7
- 上記のサイトを参考にjqueryのワイルドカードを利用したいのですが
7
+ 上記のサイトを参考にjqueryのワイルドカードを利用してモデルからjsonでデータを参照したいのですが
8
8
 
9
9
  Railsのform_forとf.fields_forを使用するとfields_forメソッドによりidの数字の連番の位置が
10
10
 
@@ -12,7 +12,7 @@
12
12
 
13
13
  <親モデル(単数系)>_<子モデル(単数系)>_atributes_0_<子モデルのカラム名>
14
14
 
15
- となってしまいます。
15
+ となってしまいワイルドカードが利用できない
16
16
 
17
17
 
18
18
 
@@ -29,6 +29,10 @@
29
29
 
30
30
 
31
31
  ###実現したいこと
32
+
33
+ jquery(または javascript)のワイルドカードを利用してモデルからjsonでデータを参照したい
34
+
35
+ または
32
36
 
33
37
  idの数字の連番が
34
38
 
@@ -50,6 +54,16 @@
50
54
 
51
55
 
52
56
 
57
+ ###試したこと
58
+
59
+
60
+
61
+ [ruby の eachメソッドでループ回数が必要な場合 (for文のiのような)](http://useless.hatenablog.com/entry/2016/07/11/190000)
62
+
63
+ のサイトのように fields_forのyyyに対してブロックの中で s を参考に連番として当てられないかと試しましたができませんでした。
64
+
65
+
66
+
53
67
  ###動作環境
54
68
 
55
69
  OS:mac
@@ -57,3 +71,115 @@
57
71
  Ruby:2.3.5
58
72
 
59
73
  Ruby on Rails: 4.2.5
74
+
75
+ ##コード
76
+
77
+ view部分
78
+
79
+ ```ruby
80
+
81
+ <%= form_for (@xxx) do |f|%>
82
+
83
+ <%= f.fields_for :yyy do | yyy, s | %>
84
+
85
+ .           <%= yyy.text_field 'code_no' %>
86
+
87
+ .           <%= yyy.text_field 'part_number' %>
88
+
89
+ <% end %>
90
+
91
+  <div class="actions">
92
+
93
+ <%= f.submit @submit %>
94
+
95
+ </div>
96
+
97
+ <% end %>
98
+
99
+ ```
100
+
101
+ js部分(jquery混同しています)
102
+
103
+ ```javascript
104
+
105
+ $('#xxx_yyy_attributes_' + s + '_code_no').change(function () {
106
+
107
+ var input = ($(this).val());
108
+
109
+ $.ajax({
110
+
111
+ url: '/xxxes/search_product',
112
+
113
+ type: 'GET',
114
+
115
+ data: ('keyword=' + input),
116
+
117
+ processData: false,
118
+
119
+ contentType: false,
120
+
121
+ dataType: 'json'
122
+
123
+ })
124
+
125
+ .done(function(data){
126
+
127
+ $(data).each(function(i, product){
128
+
129
+ $("#xxx_yyy_attributes_" + s + "_part_number").val(product.part_number)
130
+
131
+ });
132
+
133
+ });
134
+
135
+ });
136
+
137
+ ```
138
+
139
+ controller部分
140
+
141
+ ```ruby
142
+
143
+ class XxxesController < ApplicationController
144
+
145
+
146
+
147
+ # GET /xxxes/new
148
+
149
+ def new
150
+
151
+ @xxx = Xxx.new
152
+
153
+
154
+
155
+ (1..10).each do |num|
156
+
157
+ @xxx.yyy.build
158
+
159
+ end
160
+
161
+ end
162
+
163
+
164
+
165
+ # jsonでデータを取得する
166
+
167
+ def search_product
168
+
169
+ @product = Product.where('code_no LIKE(?)', "#{params[:keyword]}")
170
+
171
+ render json: @product
172
+
173
+ end
174
+
175
+ .
176
+
177
+ .
178
+
179
+
180
+
181
+ end
182
+
183
+
184
+
185
+ ```

1

タグを追加しました。

2017/12/16 12:39

投稿

begenner
begenner

スコア79

test CHANGED
File without changes
test CHANGED
File without changes