質問編集履歴
1
cmdのエラー内容追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -123,3 +123,191 @@
|
|
123
123
|
そうすると、webページに「Internal Server Error incompatible character encodings: Windows-31J and UTF-8」となってしまいました。
|
124
124
|
|
125
125
|
上手く表示させるにはどこの部分のコードを改変したらいいでしょうか?
|
126
|
+
|
127
|
+
コマンドプロンプトに下記の一つ目のファイルを入力し、その後webのアドレス欄で2つ目のファイルを実行すると「incompatible character encodings: Windows-31J and UTF-8」が発生してしまいます。
|
128
|
+
|
129
|
+
1つ目、
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
|
133
|
+
# coding: utf-8
|
134
|
+
|
135
|
+
require "webrick"
|
136
|
+
|
137
|
+
config = {
|
138
|
+
|
139
|
+
:Port => 8099,
|
140
|
+
|
141
|
+
:DocumentRoot => '.',
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
# 拡張子erbのEBRを呼び出して、EBRHandlerと関連付ける
|
148
|
+
|
149
|
+
WEBrick::HTTPServlet::FileHandler.add_handler("erb",WEBrick::HTTPServlet::ERBHandler)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
# WEBrickサーバークラスのインスタンスを作成
|
154
|
+
|
155
|
+
server = WEBrick::HTTPServer.new(config)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
# erbのMINEタイプを設定
|
160
|
+
|
161
|
+
server.config[:MimeTypes]["erb"] = "text/html"
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
# ctrl-cでサーバの停止
|
166
|
+
|
167
|
+
trap(:INT) do
|
168
|
+
|
169
|
+
server.shutdown
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
server.start
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
そして2つ目
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```erb
|
186
|
+
|
187
|
+
<%# coding: utf-8 %>
|
188
|
+
|
189
|
+
<% require "rubygems" %>
|
190
|
+
|
191
|
+
<% require "dbi" %>
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
<% dbh = DBI.connect('DBI:SQLite3:fruits01.db') %>
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
<% sth = dbh.execute("select * from products") %>
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
<html>
|
204
|
+
|
205
|
+
<body>
|
206
|
+
|
207
|
+
<ul>
|
208
|
+
|
209
|
+
<% sth.each do |row| %>
|
210
|
+
|
211
|
+
<% row.each_with_name do |val,name| %>
|
212
|
+
|
213
|
+
<% name.force_encoding("UTF-8") %>
|
214
|
+
|
215
|
+
<% val.to_s.force_encoding("UTF-8") %>
|
216
|
+
|
217
|
+
<li><%= "#{name}: #{val.to_s}" %>
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
</li>
|
222
|
+
|
223
|
+
<% end %>
|
224
|
+
|
225
|
+
<hr>
|
226
|
+
|
227
|
+
<% end %>
|
228
|
+
|
229
|
+
</ul>
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
</body>
|
234
|
+
|
235
|
+
</html>
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
<% sth.finish %>
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
<% dbh.disconnect %>
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
そうすると、webページに「Internal Server Error incompatible character encodings: Windows-31J and UTF-8」となってしまいました。
|
250
|
+
|
251
|
+
上手く表示させるにはどこの部分のコードを改変したらいいでしょうか?
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
追伸
|
256
|
+
|
257
|
+
コマンドプロントのエラーはこのようになっています。
|
258
|
+
|
259
|
+
```cmd
|
260
|
+
|
261
|
+
[2018-09-25 22:51:18] ERROR Encoding::CompatibilityError: incompatible character encodings: Windows-31J and UTF-8
|
262
|
+
|
263
|
+
(erb):16:in `concat'
|
264
|
+
|
265
|
+
(erb):16:in `block (3 levels) in evaluate'
|
266
|
+
|
267
|
+
F:/ruby2/Ruby24-x64/lib/ruby/gems/2.4.0/gems/dbi-0.4.5/lib/dbi/row.rb:82:in `block in each_with_name'
|
268
|
+
|
269
|
+
F:/ruby2/Ruby24-x64/lib/ruby/gems/2.4.0/gems/dbi-0.4.5/lib/dbi/row.rb:81:in `each'
|
270
|
+
|
271
|
+
F:/ruby2/Ruby24-x64/lib/ruby/gems/2.4.0/gems/dbi-0.4.5/lib/dbi/row.rb:81:in `each_with_index'
|
272
|
+
|
273
|
+
F:/ruby2/Ruby24-x64/lib/ruby/gems/2.4.0/gems/dbi-0.4.5/lib/dbi/row.rb:81:in `each_with_name'
|
274
|
+
|
275
|
+
(erb):13:in `block (2 levels) in evaluate'
|
276
|
+
|
277
|
+
F:/ruby2/Ruby24-x64/lib/ruby/gems/2.4.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:214:in `fetch'
|
278
|
+
|
279
|
+
F:/ruby2/Ruby24-x64/lib/ruby/gems/2.4.0/gems/dbi-0.4.5/lib/dbi/handles/statement.rb:240:in `each'
|
280
|
+
|
281
|
+
(erb):12:in `block in evaluate'
|
282
|
+
|
283
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/erb.rb:896:in `eval'
|
284
|
+
|
285
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/erb.rb:896:in `result'
|
286
|
+
|
287
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/erbhandler.rb:83:in `block in evaluate'
|
288
|
+
|
289
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/erbhandler.rb:80:in `module_eval'
|
290
|
+
|
291
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/erbhandler.rb:80:in `evaluate'
|
292
|
+
|
293
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/erbhandler.rb:57:in `do_GET'
|
294
|
+
|
295
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/abstract.rb:107:in `service'
|
296
|
+
|
297
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/filehandler.rb:307:in `exec_handler'
|
298
|
+
|
299
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/filehandler.rb:238:in `do_GET'
|
300
|
+
|
301
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/abstract.rb:107:in `service'
|
302
|
+
|
303
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpservlet/filehandler.rb:234:in `service'
|
304
|
+
|
305
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpserver.rb:140:in `service'
|
306
|
+
|
307
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/httpserver.rb:96:in `run'
|
308
|
+
|
309
|
+
F:/ruby2/Ruby24-x64/lib/ruby/2.4.0/webrick/server.rb:308:in `block in start_thread'
|
310
|
+
|
311
|
+
::1 - - [25/Sep/2018:22:51:18 東京 (標準時)] "GET /ex2605.erb HTTP/1.1" 500 335
|
312
|
+
|
313
|
+
```
|