質問編集履歴

3

追加

2015/04/03 03:20

投稿

smith
smith

スコア73

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- options_for_selectでvalidateエラーからviewに戻ってくると、選択していた値がクリアされてしまいます。
1
+ validateエラーからviewに戻ってくると、セレクトボックスで選択していた値がクリアされてしまいます。
2
2
 
3
3
  どうしたら値を保持したままでいられるでしょうか
4
4
 
@@ -14,7 +14,7 @@
14
14
 
15
15
  ```lang-ruby
16
16
 
17
- <div class="form-group">
17
+ <div class="form-group">
18
18
 
19
19
  <label for="category_id" class="col-sm-3 control-label">カテゴリ&ensp;<span class="text-danger glyphicon glyphicon-asterisk"></span></label>
20
20
 
@@ -36,4 +36,90 @@
36
36
 
37
37
 
38
38
 
39
+
40
+
41
+ <div class="form-group">
42
+
43
+ <label for="category_id" class="col-sm-3 control-label">サブカテゴリ&ensp;<span class="text-danger glyphicon glyphicon-asterisk"></span></label>
44
+
45
+ <div class="col-sm-9 col-md-3 col-lg-3">
46
+
47
+
48
+
49
+
50
+
51
+ <%= f.select :sub_category_id, options_for_select(sub_categories.collect { |child|
52
+
53
+ [child.name, child.id] }), {:include_blank => '選択してください' },
54
+
55
+ { id: 'sub_category_select' , class: 'form-control select select-default '} %>
56
+
57
+ </div>
58
+
59
+ </div>
60
+
39
61
  ```
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+ ```lang-javascript
70
+
71
+ $(document).on('change', '#category_select', function(evt) {
72
+
73
+ $.ajax({
74
+
75
+ type: 'GET',
76
+
77
+ url: '/items/list',
78
+
79
+ dataType: 'json',
80
+
81
+ data: {
82
+
83
+ category_id: $("#category_select option:selected").val()
84
+
85
+ },
86
+
87
+ error: function(jqXHR, textStatus, errorThrown) {
88
+
89
+ console.log("AJAX Error: " + textStatus);
90
+
91
+ },
92
+
93
+ success: function(data, textStatus, jqXHR) {
94
+
95
+ $("#sub_category_select").empty();
96
+
97
+ if (data.length == 0) {
98
+
99
+ $("#sub_category_select").hide();
100
+
101
+ } else {
102
+
103
+ $("#sub_category_select").show();
104
+
105
+ }
106
+
107
+ $.each(data, function(idx, sub) {
108
+
109
+ // console.log("" + item.code + ":" + item.name);
110
+
111
+ $("#sub_category_select").append('<option value="' + sub.id + '">' + sub.name + '</option>');
112
+
113
+ });
114
+
115
+ console.log("Dynamic country select OK!");
116
+
117
+ }
118
+
119
+ });
120
+
121
+ });
122
+
123
+
124
+
125
+ ```

2

修正

2015/04/03 03:20

投稿

smith
smith

スコア73

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- **ボールドテキスト**options_for_selectでvalidateエラーからviewに戻ってくると、選択していた値がクリアされてしまいます。
1
+ options_for_selectでvalidateエラーからviewに戻ってくると、選択していた値がクリアされてしまいます。
2
2
 
3
3
  どうしたら値を保持したままでいられるでしょうか
4
4
 

1

修正

2015/03/09 13:38

投稿

smith
smith

スコア73

test CHANGED
File without changes
test CHANGED
@@ -1,9 +1,39 @@
1
- collection_selectやoptions_for_selectで
2
-
3
- validateエラーからviewに戻ってくると、選択していた値がクリアされてしまいます。
1
+ **ボールドテキスト**options_for_selectでvalidateエラーからviewに戻ってくると、選択していた値がクリアされてしまいます。
4
2
 
5
3
  どうしたら値を保持したままでいられるでしょうか
6
4
 
7
5
 
8
6
 
9
- 他の項目のテキストなどはしっかり保持された状態です。
7
+ 他の項目はしっかり保持された状態です。
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ ```lang-ruby
16
+
17
+ <div class="form-group">
18
+
19
+ <label for="category_id" class="col-sm-3 control-label">カテゴリ&ensp;<span class="text-danger glyphicon glyphicon-asterisk"></span></label>
20
+
21
+ <div class="col-sm-9 col-md-3 col-lg-3">
22
+
23
+
24
+
25
+ <%= f.select :category_id, options_for_select(categories.collect { |category|
26
+
27
+ [category.name, category.id.to_s] }), {:include_blank => '選択してください' },
28
+
29
+ { id: 'category_select', class: 'form-control select select-default' } %>
30
+
31
+
32
+
33
+ </div>
34
+
35
+ </div>
36
+
37
+
38
+
39
+ ```