Ruby初心者です
るびまのCGI入門をやっているのですが、「フォームのデータを表示させる」(http://magazine.rubyist.net/?0012-CGIProgrammingForRubyBeginners-2#l10)
という項目ができません
初心者ながらprintを多用したbar_echo1.rbのプログラムはあまり綺麗ではないと感じ、ERBを使って以下のbar_echo1.erbのように書き直しました
それに伴い、bar_echo1.htmlにも少し手を加えました
これでクエリを表示できるかと思ったのですが、何も表示されませんでした
どこが間違っているかわかる方いましたら、ぜひ教えてください
(点線以下2つが自分で考えたプログラムです)
##bar_echo1.html(オリジナル)
lang
1 2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" 3 "http://www.w3.org/TR/html4/strict.dtd"> 4<html lang="ja"> 5<head> 6 <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> 7 <title>form</title> 8</head> 9<body> 10<form action="./bar_echo1.rb" method="get"> 11<div> 12フォームです。<br> 13<input name="t" value=""> 14<input type="submit" name="s" value="Button"> 15</div> 16</form> 17</body> 18</html>
##bar_echo1.html(改良後)
lang
1<!DOCTYPE HTML> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title>form</title> 6</head> 7<body> 8<form action="./bar_echo1.erb" method="get"> 9<div> 10フォームです。<br> 11<input name="t" value=""> 12<input type="submit" name="s" value="Button"> 13</div> 14</form> 15</body> 16</html>
##bar_echo1.erb
lang
1<html> 2<head></head> 3<body> 4<%= ENV['QUERY_STRING'] %> 5</body> 6</html>
追記です
erbを使いたかったのとかなり前のプログラムだったので付録(デフォルト)のwebrickでは無く自前のwebrickを書いたのですがそれが原因の気がします...
lang
1##myserver.rb(自前) 2require 'webrick' 3 4config = { 5 :Port => 8099, 6 :DocumentRoot => './', 7} 8 9WEBrick::HTTPServlet::FileHandler.add_handler("erb", WEBrick::HTTPServlet::ERBHandler) 10 11server = WEBrick::HTTPServer.new(config) 12 13server.config[:MimeTypes]["erb"] = "text/html" 14 15trap(:INT) do 16 server.shutdown 17end 18 19server.start
##server.rb(デフォルト)
lang
1require 'webrick' 2 3rrr = WEBrick::HTTPServlet::CGIHandler::Ruby 4$ruby = $ruby || rrr 5 6module WEBrick 7 module HTTPServlet 8 FileHandler.add_handler("rb", CGIHandler) 9 end 10end 11 12def start_webrick(config = {}) 13 conf = { 14 :Port => 8080, 15 :BindAddress => '127.0.0.1', 16 :CGIInterpreter => $ruby, 17 } 18 config.update(conf) 19 server = WEBrick::HTTPServer.new(config) 20 yield server if block_given? 21 ['INT', 'TERM'].each {|signal| 22 trap(signal) {server.shutdown} 23 } 24 server.start 25end 26 27start_webrick {|server| 28 cgi_dir = File.dirname( File.expand_path(__FILE__) ) 29 server.mount("/", WEBrick::HTTPServlet::FileHandler, cgi_dir, {:FancyIndexing=>true}) 30}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/04/22 17:52