teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

コード修正

2023/11/14 11:40

投稿

hokahomu
hokahomu

スコア38

title CHANGED
File without changes
body CHANGED
@@ -1,214 +1,209 @@
1
- ### 前提・実現したいこと
2
- railsで作成しているアプリケーション内で、concern置いているプログラムを通してデータベースにデータを保存したい。
3
- ### 発生している問題・エラーメッセージ
4
- concern下のプログラムが動かず、viewでNoMethodErrorが発生しています。
5
-
6
- NoMethodErrorのエラーコード
7
- ```
8
- ActionView::Template::Error (undefined method `each' for nil:NilClass):
9
- 1: <head>
10
- 2: <meta http-equiv='refresh' content='10'>
11
- 3: </head>
12
- 4: <h1>IoT交通量調査</h1>
13
- 6: <p>
14
- 7: <% @value.each do |rawdate| %>
15
- 8: [distance] <%= rawdate.distance %>
16
- 8: [time] <%= rawdate.time %><br>
17
- 9: <% end %>
18
- 10: <% @resultvalue.each do |result| %>
19
- 11: [ishuman] <%= result.ishuman %>
20
- 12: [iscar] <%= result.iscar %>
21
- 13: <% end %>
22
-
23
- app/views/address/show.html.erb:11
24
-
25
-
26
-
27
- Showing D:/rails/application/app/views/address/show.html.erb where line #11 raised:
28
-
29
- undefined method `each' for nil:NilClass
30
- ```
31
- ### 調べた内容・試した内容
32
- concern内のプログラムでresultテーブルにデータを入れたり、子クラスの中でrawdataテーブルからデータを取り出したりするプログラムを書きました。concern下のプログラムがcontroller内のinclude Processで動作してないと思われるためにviewでnomethoderrorが発生しています。
33
- 以下にcontrollerとconcern下に置かれているProcess.rbのプログラムを載せます。
34
- ### 該当のソースコード
35
-
36
- ```controller
37
- require 'net/http'
38
- require 'net/https'
39
- require 'uri'
40
- require 'json'
41
- require 'date'
42
-
43
- class AddressController < ApplicationController
44
- include Process
45
- def show
46
- params = URI.encode_www_form({token: 'd80ab730-2ba7-4225-b81f-9a4d1732c39d'})
47
- uri = URI.parse("https://api.sakura.io/datastore/v1/channels?#{params}")
48
-
49
- @query = uri.query
50
- puts "aaa"
51
- response = Net::HTTP.start(uri.host,
52
- use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE,
53
- open_timeout: 5
54
- ){|https| https.get(uri.request_uri)}
55
-
56
- begin
57
- case response
58
- # 成功した場合
59
- when Net::HTTPSuccess
60
- @result = JSON.parse(response.body)
61
- for i in 0..99 do
62
- @value = @result["results"][i]["value"]
63
- cannel = @result["results"][i]["channel"]
64
- if cannel == 0 then
65
- Rawdate.create(:time => @value)
66
- else
67
- Rawdate.create(:distance => @value)
68
- end
69
- end
70
- @size = @result["meta"]["count"]
71
- @value = Rawdate.all
72
- start_main()
73
- calculation()
74
- output()
75
-
76
- when Net::HTTPRedirection
77
- @message = "Redirection: code=#{response.code} message=#{response.message}"
78
- # その他エラー
79
- else
80
- @message = "HTTP ERROR: code=#{response.code} message=#{response.message}"
81
- end
82
- # エラー時処理
83
- rescue IOError => e
84
- @message = e.message
85
- rescue TimeoutError => e
86
- @message = e.message
87
- rescue JSON::ParserError => e
88
- @message = e.message
89
- rescue => e
90
- @message = e.message
91
- end
92
- end
93
- end
94
- ```
95
-
96
- ```Process
97
- module Process
98
-
99
- extend ActiveSupport::Concern
100
-
101
- require_relative 'read/read_main'
102
- require_relative 'object_data/object_data'
103
- require_relative 'number_data/number_data_main'
104
- require_relative 'S_D_data/S_D_data_main'
105
- require_relative 'transit_time/transit_data_main'
106
- require_relative 'range_data/range_main'
107
- require_relative 'count/count_main'
108
- require_relative 'write/write_main'
109
-
110
- def start_main
111
- file_read = Read_main.new
112
- write = Write_main.new
113
- #p file_read.start_time
114
- #p file_read.sensing_dis
115
- #p file_read.data_cnt
116
- p file_read.main
117
-
118
- object_data = Object_data.new(file_read.main, file_read.data_cnt)
119
- print "\n---------------object_data------------------\n"
120
- #p object_data.main
121
- object_data.drow
122
- print "\n--------------------------------------------\n"
123
-
124
-
125
- q_flag = 0
126
-
127
- count = Count_main.new(object_data.main)
128
-
129
- #p count.count_number #データの個数
130
- #exit
131
- end
132
-
133
- def calculation
134
- count.count_number.times do |i|
135
- weight = 0
136
- number_data = Number_main.new(object_data.main)
137
- if number_data.weight(i).class == Float
138
- weight += number_data.weight(i)
139
- else
140
- q_flag = 1
141
- end
142
-
143
- if q_flag == 0
144
- s_d = S_D_main.new(object_data.main)
145
- #p s_d.val(0)
146
- #p s_d.weight(i)
147
- if s_d.weight(i).class == Float
148
- weight += s_d.weight(i)
149
- else
150
- q_flag = 1
151
- end
152
- end
153
-
154
- if q_flag == 0
155
- transit_time = Transit_main.new(object_data.main)
156
- if transit_time.weight(i).class == Float
157
- printf("経過時間重み: %f\n", transit_time.weight(i))
158
- weight += transit_time.weight(i)
159
- else
160
- printf("経過時間重み: ?\n")
161
- q_flag = 1
162
- end
163
- end
164
-
165
- if q_flag == 0
166
- range = Range_main.new(object_data.main)
167
- #p range.val(0)
168
- if range.weight(i).class == Float
169
- printf("距離範囲重み: %f\n", range.weight(i))
170
- weight += range.weight(i)
171
- else
172
- printf("距離範囲重み: ?\n")
173
- q_flag = 1
174
- end
175
- end
176
-
177
-
178
- if q_flag == 0
179
- printf("重み合計: %f\n", weight)
180
- else
181
- printf("重み合計: ?\n")
182
- end
183
-
184
- write.count_car_or_human(weight,q_flag)
185
-
186
- q_flag = 0
187
-
188
- end
189
- end
190
-
191
- def output
192
- printf("\n---------------------result------------------------\n")
193
- printf("全体のオブジェクト個数: %d \n",count.count_number)
194
- printf("車であろう個数: %d \n",write.count_car)
195
- printf("人であろう個数: %d \n",write.count_human)
196
- printf("判定不可能なオブジェクトの個数: %d ",write.count_q)
197
- printf("\n---------------------------------------------------\n")
198
- Result.create(:ishuman => write.count_human)
199
- Result.create(:iscar => write.count_car)
200
- @resultvalue = Result.all
201
-
202
- end
203
- end
204
- ```
205
-
206
-
207
-
208
-
209
-
210
- ### 補足情報(FW/ツールのバージョンなど)
211
-
212
- ruby 2.6.5.p114
213
- rails 6.0.2.1
1
+ ### 前提・実現したいこと
2
+ railsで作成しているアプリケーション内で、concern置いているプログラムを通してデータベースにデータを保存したい。
3
+ ### 発生している問題・エラーメッセージ
4
+ concern下のプログラムが動かず、viewでNoMethodErrorが発生しています。
5
+
6
+ NoMethodErrorのエラーコード
7
+ ```
8
+ ActionView::Template::Error (undefined method `each' for nil:NilClass):
9
+ 1: <head>
10
+ 2: <meta http-equiv='refresh' content='10'>
11
+ 3: </head>
12
+ 4: <h1>title</h1>
13
+ 6: <p>
14
+ 7: <% @value.each do |rawdate| %>
15
+ 8: [distance] <%= rawdate.distance %>
16
+ 8: [time] <%= rawdate.time %><br>
17
+ 9: <% end %>
18
+ 10: <% @resultvalue.each do |result| %>
19
+ 11: [ishuman] <%= result.ishuman %>
20
+ 12: [iscar] <%= result.iscar %>
21
+ 13: <% end %>
22
+
23
+ app/views/address/show.html.erb:11
24
+
25
+
26
+
27
+ Showing D:/rails/application/app/views/address/show.html.erb where line #11 raised:
28
+
29
+ undefined method `each' for nil:NilClass
30
+ ```
31
+ ### 調べた内容・試した内容
32
+ concern内のプログラムでresultテーブルにデータを入れたり、子クラスの中でrawdataテーブルからデータを取り出したりするプログラムを書きました。concern下のプログラムがcontroller内のinclude Processで動作してないと思われるためにviewでnomethoderrorが発生しています。
33
+ 以下にcontrollerとconcern下に置かれているProcess.rbのプログラムを載せます。
34
+ ### 該当のソースコード
35
+
36
+ ```controller
37
+ require 'net/http'
38
+ require 'net/https'
39
+ require 'uri'
40
+ require 'json'
41
+ require 'date'
42
+
43
+ class AddressController < ApplicationController
44
+ include Process
45
+ def show
46
+ params = URI.encode_www_form({token: 'token'})
47
+ uri = URI.parse("https://api.sakura.io/datastore/v1/channels?#{params}")
48
+
49
+ @query = uri.query
50
+ puts "aaa"
51
+ response = Net::HTTP.start(uri.host,
52
+ use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE,
53
+ open_timeout: 5
54
+ ){|https| https.get(uri.request_uri)}
55
+
56
+ begin
57
+ case response
58
+ # 成功した場合
59
+ when Net::HTTPSuccess
60
+ @result = JSON.parse(response.body)
61
+ for i in 0..99 do
62
+ @value = @result["results"][i]["value"]
63
+ cannel = @result["results"][i]["channel"]
64
+ if cannel == 0 then
65
+ Rawdate.create(:time => @value)
66
+ else
67
+ Rawdate.create(:distance => @value)
68
+ end
69
+ end
70
+ @size = @result["meta"]["count"]
71
+ @value = Rawdate.all
72
+ start_main()
73
+ calculation()
74
+ output()
75
+
76
+ when Net::HTTPRedirection
77
+ @message = "Redirection: code=#{response.code} message=#{response.message}"
78
+ # その他エラー
79
+ else
80
+ @message = "HTTP ERROR: code=#{response.code} message=#{response.message}"
81
+ end
82
+ # エラー時処理
83
+ rescue IOError => e
84
+ @message = e.message
85
+ rescue TimeoutError => e
86
+ @message = e.message
87
+ rescue JSON::ParserError => e
88
+ @message = e.message
89
+ rescue => e
90
+ @message = e.message
91
+ end
92
+ end
93
+ end
94
+ ```
95
+
96
+ ```Process
97
+ module Process
98
+
99
+ extend ActiveSupport::Concern
100
+
101
+ require_relative 'read/read_main'
102
+ require_relative 'object_data/object_data'
103
+ require_relative 'number_data/number_data_main'
104
+ require_relative 'S_D_data/S_D_data_main'
105
+ require_relative 'transit_time/transit_data_main'
106
+ require_relative 'range_data/range_main'
107
+ require_relative 'count/count_main'
108
+ require_relative 'write/write_main'
109
+
110
+ def start_main
111
+ file_read = Read_main.new
112
+ write = Write_main.new
113
+ #p file_read.start_time
114
+ #p file_read.sensing_dis
115
+ #p file_read.data_cnt
116
+ p file_read.main
117
+
118
+ object_data = Object_data.new(file_read.main, file_read.data_cnt)
119
+ print "\n---------------object_data------------------\n"
120
+ #p object_data.main
121
+ object_data.drow
122
+ print "\n--------------------------------------------\n"
123
+
124
+
125
+ q_flag = 0
126
+
127
+ count = Count_main.new(object_data.main)
128
+
129
+ #p count.count_number #データの個数
130
+ #exit
131
+ end
132
+
133
+ def calculation
134
+ count.count_number.times do |i|
135
+ weight = 0
136
+ number_data = Number_main.new(object_data.main)
137
+ if number_data.weight(i).class == Float
138
+ weight += number_data.weight(i)
139
+ else
140
+ q_flag = 1
141
+ end
142
+
143
+ if q_flag == 0
144
+ s_d = S_D_main.new(object_data.main)
145
+ #p s_d.val(0)
146
+ #p s_d.weight(i)
147
+ if s_d.weight(i).class == Float
148
+ weight += s_d.weight(i)
149
+ else
150
+ q_flag = 1
151
+ end
152
+ end
153
+
154
+ if q_flag == 0
155
+ transit_time = Transit_main.new(object_data.main)
156
+ if transit_time.weight(i).class == Float
157
+ printf("weight: %f\n", transit_time.weight(i))
158
+ weight += transit_time.weight(i)
159
+ else
160
+ printf("weight: ?\n")
161
+ q_flag = 1
162
+ end
163
+ end
164
+
165
+ if q_flag == 0
166
+ range = Range_main.new(object_data.main)
167
+ #p range.val(0)
168
+ if range.weight(i).class == Float
169
+ printf("range: %f\n", range.weight(i))
170
+ weight += range.weight(i)
171
+ else
172
+ printf("range: ?\n")
173
+ q_flag = 1
174
+ end
175
+ end
176
+
177
+
178
+ if q_flag == 0
179
+ printf("sum: %f\n", weight)
180
+ else
181
+ printf("sum: ?\n")
182
+ end
183
+
184
+ write.count_car_or_human(weight,q_flag)
185
+
186
+ q_flag = 0
187
+
188
+ end
189
+ end
190
+
191
+ def output
192
+ printf("\n---------------------result------------------------\n")
193
+ Result.create(:ishuman => write.count_human)
194
+ Result.create(:iscar => write.count_car)
195
+ @resultvalue = Result.all
196
+
197
+ end
198
+ end
199
+ ```
200
+
201
+
202
+
203
+
204
+
205
+ ### 補足情報(FW/ツールのバージョンなど)
206
+
207
+ ruby 2.6.5.p114
208
+ rails 6.0.2.1
214
209
  sqlite3 3.30.1

4

コードの追記

2020/02/20 07:22

投稿

hokahomu
hokahomu

スコア38

title CHANGED
File without changes
body CHANGED
@@ -41,6 +41,7 @@
41
41
  require 'date'
42
42
 
43
43
  class AddressController < ApplicationController
44
+ include Process
44
45
  def show
45
46
  params = URI.encode_www_form({token: 'd80ab730-2ba7-4225-b81f-9a4d1732c39d'})
46
47
  uri = URI.parse("https://api.sakura.io/datastore/v1/channels?#{params}")
@@ -199,10 +200,6 @@
199
200
  @resultvalue = Result.all
200
201
 
201
202
  end
202
-
203
- class AddressController < ApplicationController
204
- include Process
205
- end
206
203
  end
207
204
  ```
208
205
 

3

コードの追記

2020/02/20 07:22

投稿

hokahomu
hokahomu

スコア38

title CHANGED
File without changes
body CHANGED
@@ -38,32 +38,25 @@
38
38
  require 'net/https'
39
39
  require 'uri'
40
40
  require 'json'
41
+ require 'date'
41
42
 
42
43
  class AddressController < ApplicationController
43
44
  def show
44
- params = URI.encode_www_form({token: '*********'})
45
+ params = URI.encode_www_form({token: 'd80ab730-2ba7-4225-b81f-9a4d1732c39d'})
45
- # URIを解析し、hostやportをバラバラに取得できるようにする
46
- uri = URI.parse("apiのURL?#{params}")
46
+ uri = URI.parse("https://api.sakura.io/datastore/v1/channels?#{params}")
47
47
 
48
-    @query = uri.query
48
+ @query = uri.query
49
49
  puts "aaa"
50
- # 新しくHTTPセッションを開始し、結果をresponseへ格納
51
50
  response = Net::HTTP.start(uri.host,
52
51
  use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE,
53
- #response = https.start
54
- # 接続時に待つ最大秒数を設定
55
52
  open_timeout: 5
56
- ){|https| https.get(uri.request_uri)}
53
+ ){|https| https.get(uri.request_uri)}
57
54
 
58
-
59
55
  begin
60
56
  case response
61
57
  # 成功した場合
62
58
  when Net::HTTPSuccess
63
- # responseのbody要素をJSON形式で解釈し、hashに変換
64
- puts "a"
65
59
  @result = JSON.parse(response.body)
66
- # 表示用の変数に結果を格納
67
60
  for i in 0..99 do
68
61
  @value = @result["results"][i]["value"]
69
62
  cannel = @result["results"][i]["channel"]
@@ -74,9 +67,11 @@
74
67
  end
75
68
  end
76
69
  @size = @result["meta"]["count"]
77
- @value = rawdata.all
70
+ @value = Rawdate.all
71
+ start_main()
78
- include Process
72
+ calculation()
79
- # 別のURLに飛ばされた場合
73
+ output()
74
+
80
75
  when Net::HTTPRedirection
81
76
  @message = "Redirection: code=#{response.code} message=#{response.message}"
82
77
  # その他エラー
@@ -114,6 +109,9 @@
114
109
  def start_main
115
110
  file_read = Read_main.new
116
111
  write = Write_main.new
112
+ #p file_read.start_time
113
+ #p file_read.sensing_dis
114
+ #p file_read.data_cnt
117
115
  p file_read.main
118
116
 
119
117
  object_data = Object_data.new(file_read.main, file_read.data_cnt)
@@ -135,8 +133,6 @@
135
133
  count.count_number.times do |i|
136
134
  weight = 0
137
135
  number_data = Number_main.new(object_data.main)
138
- #p number_data.weight(i).class
139
- #p number_data.weight(i)
140
136
  if number_data.weight(i).class == Float
141
137
  weight += number_data.weight(i)
142
138
  else
@@ -156,10 +152,11 @@
156
152
 
157
153
  if q_flag == 0
158
154
  transit_time = Transit_main.new(object_data.main)
159
- #p transit_time.drow(0)
160
155
  if transit_time.weight(i).class == Float
156
+ printf("経過時間重み: %f\n", transit_time.weight(i))
161
157
  weight += transit_time.weight(i)
162
158
  else
159
+ printf("経過時間重み: ?\n")
163
160
  q_flag = 1
164
161
  end
165
162
  end
@@ -168,35 +165,44 @@
168
165
  range = Range_main.new(object_data.main)
169
166
  #p range.val(0)
170
167
  if range.weight(i).class == Float
168
+ printf("距離範囲重み: %f\n", range.weight(i))
171
169
  weight += range.weight(i)
172
170
  else
171
+ printf("距離範囲重み: ?\n")
173
172
  q_flag = 1
174
173
  end
175
174
  end
175
+
176
+
177
+ if q_flag == 0
178
+ printf("重み合計: %f\n", weight)
179
+ else
180
+ printf("重み合計: ?\n")
181
+ end
182
+
176
183
  write.count_car_or_human(weight,q_flag)
177
184
 
178
185
  q_flag = 0
179
-
186
+
180
187
  end
181
188
  end
182
189
 
183
- def output
190
+ def output
184
-
185
- #write.count_ans
186
191
  printf("\n---------------------result------------------------\n")
187
192
  printf("全体のオブジェクト個数: %d \n",count.count_number)
188
193
  printf("車であろう個数: %d \n",write.count_car)
189
194
  printf("人であろう個数: %d \n",write.count_human)
190
195
  printf("判定不可能なオブジェクトの個数: %d ",write.count_q)
191
196
  printf("\n---------------------------------------------------\n")
192
- Result.create(:ishuman => count_human)
197
+ Result.create(:ishuman => write.count_human)
193
- Result.create(:iscar => count_car)
198
+ Result.create(:iscar => write.count_car)
194
199
  @resultvalue = Result.all
200
+
195
201
  end
196
202
 
197
- start_main()
203
+ class AddressController < ApplicationController
198
- calculation()
204
+ include Process
199
- output()
205
+ end
200
206
  end
201
207
  ```
202
208
 

2

コードの追記

2020/02/19 05:20

投稿

hokahomu
hokahomu

スコア38

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,15 @@
45
45
  # URIを解析し、hostやportをバラバラに取得できるようにする
46
46
  uri = URI.parse("apiのURL?#{params}")
47
47
 
48
+    @query = uri.query
49
+ puts "aaa"
50
+ # 新しくHTTPセッションを開始し、結果をresponseへ格納
51
+ response = Net::HTTP.start(uri.host,
52
+ use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE,
53
+ #response = https.start
48
- ...中略...
54
+ # 接続時に待つ最大秒数を設定
55
+ open_timeout: 5
56
+ ){|https| https.get(uri.request_uri)}
49
57
 
50
58
 
51
59
  begin
@@ -68,8 +76,23 @@
68
76
  @size = @result["meta"]["count"]
69
77
  @value = rawdata.all
70
78
  include Process
71
-
79
+ # 別のURLに飛ばされた場合
80
+ when Net::HTTPRedirection
81
+ @message = "Redirection: code=#{response.code} message=#{response.message}"
72
- ...省略...
82
+ # その他エラー
83
+ else
84
+ @message = "HTTP ERROR: code=#{response.code} message=#{response.message}"
85
+ end
86
+ # エラー時処理
87
+ rescue IOError => e
88
+ @message = e.message
89
+ rescue TimeoutError => e
90
+ @message = e.message
91
+ rescue JSON::ParserError => e
92
+ @message = e.message
93
+ rescue => e
94
+ @message = e.message
95
+ end
73
96
  end
74
97
  end
75
98
  ```

1

文章の修正

2020/02/18 09:04

投稿

hokahomu
hokahomu

スコア38

title CHANGED
File without changes
body CHANGED
@@ -6,13 +6,19 @@
6
6
  NoMethodErrorのエラーコード
7
7
  ```
8
8
  ActionView::Template::Error (undefined method `each' for nil:NilClass):
9
+ 1: <head>
10
+ 2: <meta http-equiv='refresh' content='10'>
11
+ 3: </head>
12
+ 4: <h1>IoT交通量調査</h1>
13
+ 6: <p>
14
+ 7: <% @value.each do |rawdate| %>
9
15
  8: [distance] <%= rawdate.distance %>
10
- 9: [time] <%= rawdate.time %><br>
16
+ 8: [time] <%= rawdate.time %><br>
11
- 10: <% end %>
17
+ 9: <% end %>
12
- 11: <% @resultvalue.each do |result| %>
18
+ 10: <% @resultvalue.each do |result| %>
13
- 12: [ishuman] <%= result.ishuman %>
19
+ 11: [ishuman] <%= result.ishuman %>
14
- 13: [iscar] <%= result.iscar %>
20
+ 12: [iscar] <%= result.iscar %>
15
- 14: <% end %>
21
+ 13: <% end %>
16
22
 
17
23
  app/views/address/show.html.erb:11
18
24
 
@@ -23,7 +29,8 @@
23
29
  undefined method `each' for nil:NilClass
24
30
  ```
25
31
  ### 調べた内容・試した内容
26
- concern内のプログラムでresultテーブルにデータを入れたり、子クラスの中でrawdataテーブルからデータを取り出したりするプログラムを書きました。concern下のプログラムが動作してないと思われるためにviewでnomethoderrorが発生しています。
32
+ concern内のプログラムでresultテーブルにデータを入れたり、子クラスの中でrawdataテーブルからデータを取り出したりするプログラムを書きました。concern下のプログラムがcontroller内のinclude Processで動作してないと思われるためにviewでnomethoderrorが発生しています。
33
+ 以下にcontrollerとconcern下に置かれているProcess.rbのプログラムを載せます。
27
34
  ### 該当のソースコード
28
35
 
29
36
  ```controller
@@ -67,7 +74,7 @@
67
74
  end
68
75
  ```
69
76
 
70
- ```Procces
77
+ ```Process
71
78
  module Process
72
79
 
73
80
  extend ActiveSupport::Concern