質問編集履歴
1
form_with と simple_form_for それぞれ試した結果の詳細を追記
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【Rails】
|
1
|
+
【Rails】複数条件での検索機能が実装出来ない
|
body
CHANGED
@@ -1,41 +1,77 @@
|
|
1
|
-
railsで検索機能を持ったwebアプリケーションを作成中なのですが
|
1
|
+
railsで検索機能を持ったwebアプリケーションを作成中なのですが
|
2
|
-
特にModel等はまだ仮実装的なのですが、ViewとControllerで現状問題がどこにあるのか
|
3
|
-
|
2
|
+
検索機能の実装で躓いています。
|
3
|
+
少し長くなりますが有識者の方ご教授をお願いいたします。
|
4
4
|
|
5
|
+
###実現したいこと
|
5
6
|
|
6
|
-
|
7
|
+
・複数条件でのDB検索機能実装
|
8
|
+
(最終的にはテキストボックス1、プルダウンリスト2、チェックボックス1の予定)
|
7
9
|
|
8
|
-
・
|
10
|
+
##試したこと・現状できていること
|
11
|
+
2つのformで試しましたが、それぞれ別の問題が発生しています。
|
9
12
|
|
10
|
-
**
|
13
|
+
***【form_withを使用した実装】***
|
14
|
+
・テキストボックスでの単体条件での検索と表示は実現済み
|
15
|
+
・セレクトボックス(f.select)が画面に出力されない
|
16
|
+
※f.date_field等は試したところ問題なく出力される
|
17
|
+
```View
|
18
|
+
<% @workout = Workout.new unless @workout %>
|
11
19
|
|
12
|
-
|
20
|
+
<p>検索</p>
|
13
21
|
|
22
|
+
<%= form_with(scope: :search, url: workout_index_path, method: :get, local: true) do |f| %>
|
23
|
+
<%= text_field_tag :menu %>
|
24
|
+
<%= f.select :name, ['sample1', 'sample2', 'sample3'], {include_blank: '選択ボックス'}, class: 'sample' %>
|
25
|
+
<%= submit_tag 'Search', class: "button" %>
|
14
|
-
|
26
|
+
<% end %>
|
15
27
|
|
16
|
-
|
28
|
+
<ul class="workouts">
|
29
|
+
<%= render @workouts %>
|
30
|
+
</ul>
|
17
31
|
|
18
|
-
|
32
|
+
```
|
19
33
|
|
34
|
+

|
20
35
|
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
***【simple_form_forを使用した実装】***
|
40
|
+
・テキストボックス、セレクトボックスの画面出力は実現済み
|
41
|
+
・チェックボックス(f.check_box)が出力されない
|
42
|
+
・検索が効かない(Controllerでprams[:menu]がnil)
|
43
|
+
|
21
44
|
```View
|
22
45
|
<% @workout = Workout.new unless @workout %>
|
23
46
|
|
24
47
|
<p>検索</p>
|
25
48
|
|
26
|
-
<%=
|
49
|
+
<%= simple_form_for workout_index_path,:method => 'get' do |f| %>
|
27
|
-
<%=
|
50
|
+
<%= f.input :menu %>
|
51
|
+
<%= f.label :select1 %>
|
28
|
-
<%= f.select :
|
52
|
+
<%= f.select :select1, [["1-1","1"],["1-2","2"],["1-3","3"]],
|
53
|
+
{ include_blank: true, selected: 0 }, { id: "select1", class: "browser-default" } %>
|
54
|
+
<%= f.label :select2 %>
|
55
|
+
<%= f.select :select2, [["2-1","1"],["2-2","2"],["2-3","3"]],
|
56
|
+
{ include_blank: true, selected: 0 }, { id: "select2", class: "browser-default" } %>
|
57
|
+
<%= f.label :check1 , checked: true%>
|
58
|
+
<%= f.check_box :check1 %>
|
59
|
+
<%= f.label :check2 , checked: true%>
|
60
|
+
<%= f.check_box :check2 %>
|
29
|
-
<%=
|
61
|
+
<%= f.button :submit ,"Search" %>
|
30
|
-
<% end %>
|
31
62
|
|
32
|
-
<!-- 【!TODO】pagenate-->
|
33
63
|
<ul class="workouts">
|
34
64
|
<%= render @workouts %>
|
35
65
|
</ul>
|
36
66
|
|
37
67
|
```
|
38
68
|
|
69
|
+

|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
##共通ソース(model,controller,routes)
|
74
|
+
|
39
75
|
```Controller
|
40
76
|
class WorkoutsController < ApplicationController
|
41
77
|
|