質問編集履歴

5

追記

2020/07/06 04:04

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -357,3 +357,95 @@
357
357
 
358
358
 
359
359
  ここからまったく分かりません・・・。
360
+
361
+
362
+
363
+
364
+
365
+ ***************************************************
366
+
367
+ htmlを追加
368
+
369
+ ***************************************************
370
+
371
+ 出力されてるhtmlを追記致します
372
+
373
+
374
+
375
+ ```html
376
+
377
+
378
+
379
+ <select class="select-parent" name="search[category_id]" id="search_category_id">
380
+
381
+ <option value=""></option>
382
+
383
+ <option data-children-path="/categories/1/sub_categories" value="1">野菜</option>
384
+
385
+ <option data-children-path="/categories/2/sub_categories" value="2">果物</option>
386
+
387
+ <option data-children-path="/categories/3/sub_categories" value="3">お肉</option>
388
+
389
+ <option data-children-path="/categories/4/sub_categories" value="4">魚介</option>
390
+
391
+ </select>
392
+
393
+
394
+
395
+ <select class="select-children" name="search[sub_category_id]" id="search_sub_category_id">
396
+
397
+ <option value=""></option>
398
+
399
+ <option value="1">キャベツ</option>
400
+
401
+ <option value="2">人参</option>
402
+
403
+ <option value="3">玉ねぎ</option>
404
+
405
+ <option value="4">ブロッコリー</option>
406
+
407
+ <option value="5">もやし</option>
408
+
409
+ <option value="6">レタス</option>
410
+
411
+ <option value="7">トマト</option>
412
+
413
+ </select>
414
+
415
+
416
+
417
+ ```
418
+
419
+
420
+
421
+ 以下は登録画面のhtmlです。
422
+
423
+ こちらでも同じセレクトボックス連動を使用しており、こちらは正常に動作済みです。
424
+
425
+ ```html
426
+
427
+
428
+
429
+ <select class="select-parent" name="item[category_id]" id="item_category_id">
430
+
431
+ <option value="">選択して下さい</option>
432
+
433
+ <option data-children-path="/categories/1/sub_categories" value="1">野菜</option>
434
+
435
+ <option data-children-path="/categories/2/sub_categories" value="2">果物</option>
436
+
437
+ <option data-children-path="/categories/3/sub_categories" value="3">お肉</option>
438
+
439
+ <option data-children-path="/categories/4/sub_categories" value="4">魚介</option>
440
+
441
+ </select>
442
+
443
+
444
+
445
+ <select class="select-children" name="item[sub_category_id]" id="item_sub_category_id">
446
+
447
+ <option value="">選択して下さい</option>
448
+
449
+ </select>
450
+
451
+ ```

4

調べたこと

2020/07/06 04:04

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -323,3 +323,37 @@
323
323
  'change': replaceChildrenOptions
324
324
 
325
325
  ```
326
+
327
+
328
+
329
+
330
+
331
+ ***************************************************
332
+
333
+ 分かった所までを追加
334
+
335
+ ***************************************************
336
+
337
+ その後分かった所までを追記致します。
338
+
339
+
340
+
341
+ エラーログはブラウザ、サーバともに出てませんでした。
342
+
343
+
344
+
345
+ ただ、正常な流れは、
346
+
347
+ 親(Category)のセレクトボックスを変更で
348
+
349
+ スクリプトのreplaceChildrenOptions = ->に飛ぶのに対し、
350
+
351
+ 今回のこの画面は、
352
+
353
+ replaceChildrenOptions = ->に飛びません。
354
+
355
+ replaceChildrenOptions = ->の真下にalertで確認しました。
356
+
357
+
358
+
359
+ ここからまったく分かりません・・・。

3

追記

2020/07/06 03:42

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -211,3 +211,115 @@
211
211
 
212
212
 
213
213
  ```
214
+
215
+
216
+
217
+
218
+
219
+ ***************************************************
220
+
221
+ 不足情報を追加
222
+
223
+ ***************************************************
224
+
225
+ コントローラ
226
+
227
+ ```ruby
228
+
229
+
230
+
231
+ class SubCategoriesController < ApplicationController
232
+
233
+ def index
234
+
235
+
236
+
237
+ @sub_categories = SubCategory.where(category_id: params[:category_id])
238
+
239
+
240
+
241
+ respond_to do |format|
242
+
243
+ format.html do
244
+
245
+ end
246
+
247
+
248
+
249
+ format.js do
250
+
251
+ render json: @sub_categories.select(:id, :name)
252
+
253
+ end
254
+
255
+ end
256
+
257
+
258
+
259
+ end
260
+
261
+ end
262
+
263
+
264
+
265
+ ```
266
+
267
+
268
+
269
+ coffeescript
270
+
271
+ ```coffeescript
272
+
273
+ $(document).on 'turbolinks:load', ->
274
+
275
+ replaceSelectOptions = ($select, results) ->
276
+
277
+ $select.html $('<option>')
278
+
279
+ $.each results, ->
280
+
281
+ option = $('<option>').val(this.id).text(this.name)
282
+
283
+ $select.append(option)
284
+
285
+
286
+
287
+ replaceChildrenOptions = ->
288
+
289
+ childrenPath = $(@).find('option:selected').data().childrenPath
290
+
291
+ $selectChildren = $(@).closest('form').find('.select-children')
292
+
293
+ if childrenPath?
294
+
295
+ $.ajax
296
+
297
+ url: childrenPath
298
+
299
+ dataType: "json"
300
+
301
+ success: (results) ->
302
+
303
+ replaceSelectOptions($selectChildren, results)
304
+
305
+ error: (XMLHttpRequest, textStatus, errorThrown) ->
306
+
307
+ console.error("Error occurred in replaceChildrenOptions")
308
+
309
+ console.log("XMLHttpRequest: #{XMLHttpRequest.status}")
310
+
311
+ console.log("textStatus: #{textStatus}")
312
+
313
+ console.log("errorThrown: #{errorThrown}")
314
+
315
+ else
316
+
317
+ replaceSelectOptions($selectChildren, [])
318
+
319
+
320
+
321
+ $('.select-parent').on
322
+
323
+ 'change': replaceChildrenOptions
324
+
325
+ ```

2

model追加

2020/07/05 21:24

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -161,3 +161,53 @@
161
161
 
162
162
 
163
163
  ```
164
+
165
+
166
+
167
+ アイテムモデル※検索したい対象
168
+
169
+ ```ruby
170
+
171
+ class Item < ApplicationRecord
172
+
173
+
174
+
175
+ belongs_to :category
176
+
177
+ belongs_to :sub_category
178
+
179
+
180
+
181
+
182
+
183
+ scope :search, ->(search_params) do
184
+
185
+ return if search_params.blank?
186
+
187
+
188
+
189
+ keyword_like(search_params[:keyword])
190
+
191
+ .category_id_is(search_params[:category_id])
192
+
193
+ .sub_category_id_is(search_params[:sub_category_id])
194
+
195
+ end
196
+
197
+ scope :keyword_like, -> (keyword) { where('description LIKE ? or title LIKE ?', "%#{keyword}%","%#{keyword}%") if keyword.present? }
198
+
199
+ scope :category_id_is, -> (category_id) { where(category_id: category_id) if category_id.present? }
200
+
201
+ scope :sub_category_id_is, -> (sub_category_id) { where(sub_category_id: sub_category_id) if sub_category_id.present? }
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ end
210
+
211
+
212
+
213
+ ```

1

カテゴリとサブカテゴリのモデルを追記

2020/07/05 04:28

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -111,3 +111,53 @@
111
111
 
112
112
 
113
113
  初心者につき的外れの質問しているかもしれませんが、どうかアドバイス宜しくお願いします。
114
+
115
+
116
+
117
+
118
+
119
+ ***************************************************
120
+
121
+ ご指摘により
122
+
123
+ 以下から不足情報を追加
124
+
125
+ ***************************************************
126
+
127
+
128
+
129
+ カテゴリモデル
130
+
131
+ ```ruby
132
+
133
+ class Category < ApplicationRecord
134
+
135
+ has_many :sub_categories
136
+
137
+ has_many :items
138
+
139
+ default_scope -> { order(:order_number) }
140
+
141
+ end
142
+
143
+
144
+
145
+ ```
146
+
147
+ サブカテゴリモデル
148
+
149
+ ```ruby
150
+
151
+ class SubCategory < ApplicationRecord
152
+
153
+ belongs_to :category
154
+
155
+ has_many :items
156
+
157
+ default_scope -> { order(:order_number) }
158
+
159
+ end
160
+
161
+
162
+
163
+ ```