質問編集履歴

2

情報の追記

2020/11/12 12:55

投稿

hurousyotoku500
hurousyotoku500

スコア27

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,338 @@
24
24
 
25
25
  #/rounds_controller.rb
26
26
 
27
+ class RoundsController < ApplicationController
28
+
29
+ require 'securerandom'
30
+
31
+
32
+
33
+ def index
34
+
35
+
36
+
37
+ end
38
+
39
+ def select_course
40
+
41
+
42
+
43
+ @todoufukens = {
44
+
45
+ 0 => "全地域",
46
+
47
+ 101 => "北海道・東北のすべて",
48
+
49
+ 1 => "北海道",
50
+
51
+ 2 => "青森県",
52
+
53
+ 3 => "岩手県",
54
+
55
+ 4 => "宮城県",
56
+
57
+ 5 => "秋田県",
58
+
59
+ 6 => "山形県",
60
+
61
+ 7 => "福島県",
62
+
63
+ 102 => "関東のすべて",
64
+
65
+ 8 => "茨城県",
66
+
67
+ 9 => "栃木県",
68
+
69
+ 10 => "群馬県",
70
+
71
+ 11 => "埼玉県",
72
+
73
+ 12 => "千葉県",
74
+
75
+ 13 => "東京都",
76
+
77
+ 14 => "神奈川県",
78
+
79
+ 19 => "山梨県",
80
+
81
+ 20 => "長野県",
82
+
83
+ 22 => "静岡県",
84
+
85
+ 103 => "北陸のすべて",
86
+
87
+ 15 => "新潟県",
88
+
89
+ 16 => "富山県",
90
+
91
+ 17 => "石川県",
92
+
93
+ 18 => "福井県",
94
+
95
+ 104 => "中部のすべて",
96
+
97
+ 21 => "岐阜県",
98
+
99
+ 23 => "愛知県",
100
+
101
+ 24 => "三重県",
102
+
103
+ 105 => "近畿のすべて",
104
+
105
+ 25 => "滋賀県",
106
+
107
+ 26 => "京都府",
108
+
109
+ 27 => "大阪府",
110
+
111
+ 28 => "兵庫県",
112
+
113
+ 29 => "奈良県",
114
+
115
+ 30 => "和歌山県",
116
+
117
+ 106 => "中国のすべて",
118
+
119
+ 31 => "鳥取県",
120
+
121
+ 32 => "島根県",
122
+
123
+ 33 => "岡山県",
124
+
125
+ 34 => "広島県",
126
+
127
+ 35 => "山口県",
128
+
129
+ 107 => "四国のすべて",
130
+
131
+ 36 => "徳島県",
132
+
133
+ 37 => "香川県",
134
+
135
+ 38 => "愛媛県",
136
+
137
+ 39 => "高知県",
138
+
139
+ 108 => "九州・沖縄のすべて",
140
+
141
+ 40 => "福岡県",
142
+
143
+ 41 => "佐賀県",
144
+
145
+ 42 => "長崎県",
146
+
147
+ 43 => "熊本県",
148
+
149
+ 44 => "大分県",
150
+
151
+ 45 => "宮崎県",
152
+
153
+ 46 => "鹿児島県",
154
+
155
+ 47 => "沖縄県",
156
+
157
+ 109 => "海外"
158
+
159
+ }
160
+
161
+ end
162
+
163
+
164
+
165
+ def search
166
+
167
+ require 'net/http'
168
+
169
+ require 'json'
170
+
171
+ require 'uri'
172
+
173
+
174
+
175
+ @todoufukens = {
176
+
177
+ 0 => "全地域",
178
+
179
+ 101 => "北海道・東北のすべて",
180
+
181
+ 1 => "北海道",
182
+
183
+ 2 => "青森県",
184
+
185
+ 3 => "岩手県",
186
+
187
+ 4 => "宮城県",
188
+
189
+ 5 => "秋田県",
190
+
191
+ 6 => "山形県",
192
+
193
+ 7 => "福島県",
194
+
195
+ 102 => "関東のすべて",
196
+
197
+ 8 => "茨城県",
198
+
199
+ 9 => "栃木県",
200
+
201
+ 10 => "群馬県",
202
+
203
+ 11 => "埼玉県",
204
+
205
+ 12 => "千葉県",
206
+
207
+ 13 => "東京都",
208
+
209
+ 14 => "神奈川県",
210
+
211
+ 19 => "山梨県",
212
+
213
+ 20 => "長野県",
214
+
215
+ 22 => "静岡県",
216
+
217
+ 103 => "北陸のすべて",
218
+
219
+ 15 => "新潟県",
220
+
221
+ 16 => "富山県",
222
+
223
+ 17 => "石川県",
224
+
225
+ 18 => "福井県",
226
+
227
+ 104 => "中部のすべて",
228
+
229
+ 21 => "岐阜県",
230
+
231
+ 23 => "愛知県",
232
+
233
+ 24 => "三重県",
234
+
235
+ 105 => "近畿のすべて",
236
+
237
+ 25 => "滋賀県",
238
+
239
+ 26 => "京都府",
240
+
241
+ 27 => "大阪府",
242
+
243
+ 28 => "兵庫県",
244
+
245
+ 29 => "奈良県",
246
+
247
+ 30 => "和歌山県",
248
+
249
+ 106 => "中国のすべて",
250
+
251
+ 31 => "鳥取県",
252
+
253
+ 32 => "島根県",
254
+
255
+ 33 => "岡山県",
256
+
257
+ 34 => "広島県",
258
+
259
+ 35 => "山口県",
260
+
261
+ 107 => "四国のすべて",
262
+
263
+ 36 => "徳島県",
264
+
265
+ 37 => "香川県",
266
+
267
+ 38 => "愛媛県",
268
+
269
+ 39 => "高知県",
270
+
271
+ 108 => "九州・沖縄のすべて",
272
+
273
+ 40 => "福岡県",
274
+
275
+ 41 => "佐賀県",
276
+
277
+ 42 => "長崎県",
278
+
279
+ 43 => "熊本県",
280
+
281
+ 44 => "大分県",
282
+
283
+ 45 => "宮崎県",
284
+
285
+ 46 => "鹿児島県",
286
+
287
+ 47 => "沖縄県",
288
+
289
+ 109 => "海外"
290
+
291
+ }
292
+
293
+ base_url = "https://app.rakuten.co.jp/services/api/Gora/GoraGolfCourseSearch/20170623"
294
+
295
+
296
+
297
+ k = params[:keyword]
298
+
299
+ t = params[:todoufuken]
300
+
301
+
302
+
303
+ params = {
304
+
305
+ 'keyword' => URI.encode(k),
306
+
307
+ 'areaCode' => t,
308
+
309
+ 'format' => 'json',
310
+
311
+ 'applicationId' => '1083401508369226218',
312
+
313
+ 'hits' => 30
314
+
315
+ }
316
+
317
+
318
+
319
+ uri = URI(base_url + '?' + params.map{|k,v| "#{k}=#{v}"}.join('&'))
320
+
321
+ puts uri
322
+
323
+
324
+
325
+ # begin
326
+
327
+ response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
328
+
329
+ http.open_timeout = 5
330
+
331
+ http.read_timeout = 10
332
+
333
+ http.get(uri.request_uri)
334
+
335
+ end
336
+
337
+
338
+
339
+ # case response
340
+
341
+ # when Net::HTTPSuccess
342
+
343
+ @json = JSON.parse(response.body)
344
+
345
+ # end
346
+
347
+
348
+
349
+ # rescue => e
350
+
351
+ # Rails.logger.error(e.message)
352
+
353
+ # raise e
354
+
355
+ # end
356
+
357
+ end
358
+
27
359
  def show # 1〜18H分ゴルフ場の名前をpostし、レコードを生成する処理(hole_scoreはNull)
28
360
 
29
361
  @coursename = params[:golfCourseName]
@@ -32,16 +364,56 @@
32
364
 
33
365
  round_id = SecureRandom.hex(8)
34
366
 
35
- @hoge = round_id
367
+ # hoge = params[:round_id]
36
368
 
37
369
  [*1..18].each do|num|
38
370
 
39
371
  Score.create!(course: @coursename,hole_number: num,user_id: 1,round_id: round_id,hole_score: 0)
40
372
 
373
+
374
+
375
+ # redirect_to home_index_path
376
+
377
+
378
+
41
379
  end
42
380
 
43
381
  end
44
382
 
383
+ def play
384
+
385
+ require "date"
386
+
387
+ @day = Date.today
388
+
389
+
390
+
391
+ @coursename = params[:round_id]
392
+
393
+ @score_card_score = Score.where(round_id: params[:round_id]) #ホールごとのスコアを取得
394
+
395
+ @score_card_course = Score.where(round_id: params[:round_id]).first #ラウンドしたコース、日付を取得
396
+
397
+ @score_sum = Score.where(round_id: params[:round_id]).sum(:hole_score)
398
+
399
+
400
+
401
+ #ランキング関係
402
+
403
+ @rank_user = Score.distinct.pluck(:course, :user_id, :round_id)
404
+
405
+ @rank_today = Score.where(course: params[:course]).where("created_at >= ?", Date.today).group(:round_id).count
406
+
407
+ @rank_today2 = Score.group(:round_id).count
408
+
409
+ # @rank_today3 = Score.find_by(course: params[:course], created_at >= ?", Time.zone.now.beginning_of_day)
410
+
411
+ end
412
+
413
+ end
414
+
415
+
416
+
45
417
  ```
46
418
 
47
419
 

1

情報の追記

2020/11/12 12:55

投稿

hurousyotoku500
hurousyotoku500

スコア27

test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,16 @@
1
+ コントローラにて、.createでレコードを生成し、その際にSecureRandomでIDも発行しているのですが、その値をビューのリンク先に渡したいが、渡し方がわからない。
2
+
3
+ URL直接入力にて、ルーティングは問題なく、/rounds/play/:round_idを入力してやれば表示はできております。
4
+
5
+ 情報不足かもしれませんが、宜しくお願い致します。
6
+
7
+
8
+
1
9
  ```rb
2
10
 
3
11
  #search.html.erb
4
12
 
5
13
  <% @json["Items"].each do|item| %>
6
-
7
-
8
14
 
9
15
    <%= link_to item["Item"]["golfCourseName"] , round_play_path(round_id: "ここがわからない") %> <br>
10
16
 
@@ -32,8 +38,6 @@
32
38
 
33
39
  Score.create!(course: @coursename,hole_number: num,user_id: 1,round_id: round_id,hole_score: 0)
34
40
 
35
- # redirect_to home_index_path
36
-
37
41
  end
38
42
 
39
43
  end
@@ -44,6 +48,8 @@
44
48
 
45
49
  ```rb
46
50
 
51
+ #routes.rb
52
+
47
53
  post "/rounds/search", to: 'rounds#search'
48
54
 
49
55
  get '/rounds/search/:golfCourseName', to: 'rounds#show' ,as: "round_start"