WEBrickサーバで eRuby を動かしてみる
Web ブラウザで,eRuby が埋め込まれた HTML ファイルを開くと,何かの「動く」Web ページが表示される. これを行ないたい. この目的では apache は使いづらい(ように感じる)ので,WEBrickサーバを使う
erb ファイルを実行してみる
【ここで行うこと】
- Rubyのプログラム内で“webrick”をrequireして,WEBrickサーバを起動
- ブラウザでerbファイルの実行
- WEBrick サーバを起動する Ruby プログラム
ファイル名:webserver.rb
#! ruby -Ku # -*- coding: utf-8 -*- require "webrick" config = { :Port => 8099, :DocumentRoot => '.' } WEBrick::HTTPServlet::FileHandler.add_handler("erb", WEBrick::HTTPServlet::ERBHandler) s = WEBrick::HTTPServer.new(config) s.config[:MimeTypes]["erb"] = "text/html" trap(:INT){ s.shutdown } s.start
- 「WEBrick サーバを起動する Ruby プログラム」の実行
- Web ブラウザで開く
Web ブラウザで, http://localhost:8099 を開く
- 結果の確認
ファイルの一覧が表示される(普通の Web サーバと同じ振る舞い).
* 先程のプログラム webserver.rb を,C:/Ruby/programs/webというディレクトリに置いている場合には,そのディレクトリ内のファイルの一覧が表示される.
- erb プログラムの見本
◇ hello.erb
<%= "Hello, World!!" %>
* 実行結果の例
◇ sample1.erb
<% a = 1 b = 2 c = 3 %> A = <%= a %><br /> B = <%= b %><br /> C = <%= c %>
* 実行結果の例
◇ sample2.erb
<% 10.times do |i| %> <% a = rand(10) %> <% b = rand(10) %> (<%= i+1 %>) <%= a %> + <%= b %> = <%= a + b %><br /> <% end %>
* 実行結果の例
フォームを使ってみる
- WEBrick サーバを起動する Ruby プログラム
ファイル名:webserver.rb
#! ruby -Ku # -*- coding: utf-8 -*- require "rubygems" require "webrick" require "erb" config = { :Port => 8099, :DocumentRoot => '.' } WEBrick::HTTPServlet::FileHandler.add_handler("erb", WEBrick::HTTPServlet::ERBHandler) s = WEBrick::HTTPServer.new(config) s.config[:MimeTypes]["erb"] = "text/html" s.mount_proc("/hello2") { |req, res| p req.query name = req.query["name"] template = ERB.new( File.read('hello2.erb') ) res.body << template.result( binding ) } trap(:INT){ s.shutdown } s.start
- submit.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; initial-scale=1.0; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="MSThemeCompatible" CONTENT="yes" /> <title> Sample </title> </head> <body> <form action="hello2" method='post'> <input type='text' name='name' size=10/> <br/> <input type='submit' name='submit' value='submit'/> </form> </body> </html>
- hello2.erb
<?xml version="1.0" encoding="UTF-8"?> <% # coding: UTF-8 %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; initial-scale=1.0; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="MSThemeCompatible" CONTENT="yes" /> <title> Sample </title> </head> <body> hello, <%= name %> </body> </html>
- 「WEBrick サーバを起動する Ruby プログラム」の実行
- Web ブラウザで開く
Web ブラウザで, http://localhost:8099/submit.html を開く
- 結果の確認
Ruby on Rails バージョン 3 で実行してみる(書きかけ)
Ruby on Railsのアプリケーション内のビューファイルとしてerbファイルを実行する方法.
- 先程の hello.erb. sample1.erb, sample2.erb をビューファイルとして使用.
- Rails3.0-betaとRuby1.8.7を使用.
- アプリケーション作成後、コントローラーを生成.
今回は、hello2というアプリケーションを作成し、コントローラー名はmainとした.

C:/Ruby/railsproject/hello2に移動して、WEBrickサーバを起動.
【Rails3 での注意点】 WEBrickサーバの起動には「ruby script/server」というコマンドを使っていたが、Rsils3では「rails server」または「rails s」 に変更されている.

最初に「http://localhost:3000/main/hello」を入力して実行しようとしたところ、Routing Errorというエラーが起きた.
原因は、アプリケーションを作成した際にできる、/アプリケーション名/config/routes.rbというファイルの内容が全てコメントアウトされていたため.
「match ':controller(/:action(/:id(.:format)))'」という記述のコメントアウトをはずすことで解決.