質問編集履歴

1

説明がわかりにくかったので書き換えました。

2017/11/09 05:50

投稿

yoshimitsu41
yoshimitsu41

スコア36

test CHANGED
@@ -1 +1 @@
1
- railsでグループ化した情報を出力する方法
1
+ railsでeachで一定の値でグループ化した情報を出力する方法
test CHANGED
@@ -4,45 +4,37 @@
4
4
 
5
5
 
6
6
 
7
- 路線名をグープ化?して出力したいと思っているのですがうまく動作しません
7
+ 都道府県、路線名が別々もモデになっておりアソシエーションを組んおります。
8
-
9
-
10
-
11
- ---
12
-
13
- stasion(駅名)のテーブルには
14
-
15
- id
16
-
17
- station_name|エキメイ
18
-
19
- line_id|路線のID
20
-
21
-
22
-
23
- ---
24
-
25
- line(路線名)のテーブルには
26
-
27
- id
28
-
29
- station_name|エキメイ
30
-
31
- line_id|路線のID
32
-
33
-
34
-
35
- ---
36
-
37
-
38
-
39
- があります。
40
8
 
41
9
 
42
10
 
43
11
  ```rugy
44
12
 
13
+ #########モデル#########
14
+
15
+ #pref.rb(都道府県)
16
+
17
+ class Pref < ApplicationRecord
18
+
19
+ has_many :stations
20
+
21
+ end
22
+
23
+
24
+
25
+ #station.rb(駅名)
26
+
27
+ class Station < ApplicationRecord
28
+
29
+ belongs_to :pref
30
+
31
+ belongs_to :line, foreign_key: 'line_id', primary_key: 'line_id'
32
+
33
+ end
34
+
35
+
36
+
45
- #line.rb
37
+ #line.rb(路線名)
46
38
 
47
39
  class Line < ApplicationRecord
48
40
 
@@ -58,11 +50,25 @@
58
50
 
59
51
  ```rugy
60
52
 
53
+ #########コントローラー#########
54
+
61
- #station.rb
55
+ #stations_controller.rb
62
-
56
+
63
- class Station < ApplicationRecord
57
+ class StationsController < ApplicationController
58
+
64
-
59
+ def index
60
+
65
- belongs_to :line, foreign_key: 'line_id', primary_key: 'line_id'
61
+ @pref_name = Station.find_by(pref_id: params[:area_id])
62
+
63
+ @stations = Station.where(pref_id: params[:area_id])
64
+
65
+ end
66
+
67
+
68
+
69
+ def show
70
+
71
+ end
66
72
 
67
73
 
68
74
 
@@ -72,136 +78,150 @@
72
78
 
73
79
  ```
74
80
 
75
- でアソシエーションを組んでいます。
81
+
76
-
77
-
78
-
79
-
80
-
81
- stations_controller.rbは…
82
+
82
-
83
- ```ruby
83
+ ```rugy
84
+
84
-
85
+ #########ビュー#########
86
+
87
+ #stations/index.html.erb
88
+
89
+
90
+
91
+ <div class="container">
92
+
93
+ <div class="row">
94
+
95
+ <div class="col-md-12">
96
+
97
+ <h1><%= @pref_name.pref.name %>|<%= @stations.count %>駅|Stations#index</h1>
98
+
99
+ <p>Find me in app/views/stations/index.html.erb</p>
100
+
101
+
102
+
85
- class StationsController < ApplicationController
103
+ <% @stations.each do |station| %>
86
-
87
- def index
104
+
88
-
89
- @pref_name = Station.find_by(pref_id: params[:area_id])
90
-
91
- @stations = Station.where(pref_id: params[:area_id])
105
+ <div class="panel panel-default">
106
+
92
-
107
+ <div class="panel-heading"><%= station.line.line_name %></div>
108
+
109
+ <ul class="list-group">
110
+
111
+ <li class="list-group-item"><%= station.station_name %></li>
112
+
113
+ </ul>
114
+
93
- end
115
+ </div>
94
-
95
-
96
-
116
+
97
- def show
117
+ <% end %>
98
-
118
+
99
- end
119
+ </div>
100
-
101
-
102
-
103
-
104
-
120
+
105
- end
121
+ </div>
122
+
123
+ </div>
106
124
 
107
125
 
108
126
 
109
127
  ```
110
128
 
111
- lines_controller.rbは
112
-
113
- ```ruby
114
-
115
- class LinesController < ApplicationController
116
-
117
- def index
118
-
119
- end
120
-
121
-
122
-
123
- def show
124
-
125
- end
126
-
127
- end
128
-
129
-
130
-
131
-
132
-
133
- ```
134
-
135
-
136
-
137
-
138
-
139
- 結果的にstationindex.html.erbで表示したいのが
140
-
141
- |路線名1|
142
-
143
- |駅名1|
144
-
145
- |駅名2|
146
-
147
- |駅名2|
148
-
149
-
150
-
151
- |路線名2|
152
-
153
- |駅名1|
154
-
155
- |駅名2|
156
-
157
- |駅名2|
158
-
159
-
160
-
161
- |路線名3|
162
-
163
- |駅名1|
164
-
165
- |駅名2|
166
-
167
- |駅名2|
168
-
169
- といったようにカテゴリごとに切り分けて表示したいのですがどのようにすればいいかわかりません。。。。
170
-
171
-
172
-
173
- 現状出来ているコードは
174
-
175
-
176
-
177
- ```ruby
178
-
179
- <% @stations.each do |station| %>
180
-
181
- <div class="panel panel-default">
182
-
183
- <!-- Default panel contents -->
184
-
185
- <div class="panel-heading"><%= station.line.line_name %></div>
186
-
187
- <!-- List group -->
188
-
189
- <ul class="list-group">
190
-
191
- <li class="list-group-item"><%= station.station_name %></li>
192
-
193
- </ul>
194
-
195
- </div>
196
-
197
- <% end %>
198
-
199
- ```
200
-
201
-
202
-
203
- す。
204
-
205
-
206
-
207
- ご教授お願い致します。
129
+
130
+
131
+ DBはmysqlで
132
+
133
+ prefsテーブル
134
+
135
+ ---------------
136
+
137
+ id:integer
138
+
139
+ pref_cd:integer
140
+
141
+ name:string
142
+
143
+
144
+
145
+ stationsテーブル
146
+
147
+ ---------------
148
+
149
+ id:integer
150
+
151
+ pref_id:integer
152
+
153
+ station_cd:integer
154
+
155
+ station_name:string
156
+
157
+ line_id:integer
158
+
159
+
160
+
161
+ linesテーブル
162
+
163
+ ---------------
164
+
165
+ id:integer
166
+
167
+ line_id:integer
168
+
169
+ line_name:string
170
+
171
+
172
+
173
+
174
+
175
+ 現状は下記のような表示になってしまっています。
176
+
177
+ ![イメージ説明](a81b8322f6e65cbdf944d240f44f306c.jpeg)
178
+
179
+
180
+
181
+ - 【山手線】
182
+
183
+ - 大崎
184
+
185
+ - 【山手線】
186
+
187
+ - 五反田
188
+
189
+ - 【山手線】
190
+
191
+ - 目黒
192
+
193
+
194
+
195
+ 上記のようになっているのを…
196
+
197
+
198
+
199
+ - 【山手線】
200
+
201
+ - 大崎
202
+
203
+ - 五反田
204
+
205
+ - 目黒
206
+
207
+ - 山手線の駅が全部出力されると…
208
+
209
+ - 【南武線】
210
+
211
+ - 矢野口
212
+
213
+ - 稲城長沼
214
+
215
+ - 南多摩
216
+
217
+ と言った具合に路線でグルーピング?して出力したいです。
218
+
219
+ 各駅が持っているline_id(線路のID)をグループ化してどうにかしてeachで回せないかと考えていますが答えがでません。
220
+
221
+ どのように処理すれば解決しますしょうか???
222
+
223
+
224
+
225
+
226
+
227
+ ぜひともご教授お願い致します!!!