Merb で HelloWorld を作る
まずは merb-gen コマンドでアプリケーションの雛形を作成。
$ merb-gen app helloworld
Generating with app generator:
[ADDED] tasks/merb.thor
[ADDED] .gitignore
[ADDED] public/.htaccess
[ADDED] tasks/doc.thor
[ADDED] public/javascripts/jquery.js
[ADDED] doc/rdoc/generators/merb_generator.rb
[ADDED] doc/rdoc/generators/template/merb/api_grease.js
[ADDED] doc/rdoc/generators/template/merb/index.html.erb
[ADDED] doc/rdoc/generators/template/merb/prototype.js
[ADDED] doc/rdoc/generators/template/merb/merb.css
[ADDED] doc/rdoc/generators/template/merb/merb.rb
[ADDED] doc/rdoc/generators/template/merb/merb_doc_styles.css
[ADDED] public/merb.fcgi
[ADDED] public/images/merb.jpg
[ADDED] public/favicon.ico
[ADDED] public/robots.txt
[ADDED] gems
[ADDED] Rakefile
[ADDED] app/helpers/global_helpers.rb
[ADDED] app/views/exceptions/not_found.html.erb
[ADDED] app/views/exceptions/not_acceptable.html.erb
[ADDED] app/controllers/application.rb
[ADDED] app/controllers/exceptions.rb
[ADDED] app/models/user.rb
[ADDED] autotest/discover.rb
[ADDED] autotest/merb.rb
[ADDED] autotest/merb_rspec.rb
[ADDED] config/environments/staging.rb
[ADDED] config/environments/test.rb
[ADDED] config/environments/rake.rb
[ADDED] config/environments/development.rb
[ADDED] config/environments/production.rb
[ADDED] config/database.yml
[ADDED] config/dependencies.rb
[ADDED] config/router.rb
[ADDED] config/rack.rb
[ADDED] config/init.rb
[ADDED] public/javascripts/application.js
[ADDED] public/stylesheets/master.css
[ADDED] merb/merb-auth/setup.rb
[ADDED] merb/merb-auth/strategies.rb
[ADDED] merb/session/session.rb
[ADDED] spec
[ADDED] app/views/layout/application.html.erb作成されたディレクトリに移動します。
$ cd helloworld
この状態で作成したアプリケーションを起動してみます。
$ merb Loading init file from /home/jkikuchi/src/helloworld/config/init.rb Loading /home/jkikuchi/src/helloworld/config/environments/development.rb ~ Connecting to database... ~ Loaded slice 'MerbAuthSlicePassword' ... ~ Parent pid: 15291 ~ Compiling routes... ~ Activating slice 'MerbAuthSlicePassword' ... merb : worker (port 4000) ~ Starting Mongrel at port 4000 merb : worker (port 4000) ~ Successfully bound to port 4000
ポート番号 4000 でサーバーが起動されるので、ブラウザでアクセスしてみると Merb のエラーメッセージが表示されます。Merb は問題なく動いているようですね。
Exception: No routes match the request: /
次にコントローラーを作成します。
$ merb-gen controller helloworld
Loading init file from /home/jkikuchi/src/helloworld/config/init.rb
Loading /home/jkikuchi/src/helloworld/config/environments/development.rb
Generating with controller generator:
Loading init file from /home/jkikuchi/src/helloworld/config/init.rb
Loading /home/jkikuchi/src/helloworld/config/environments/development.rb
[ADDED] app/controllers/helloworld.rb
[ADDED] app/views/helloworld/index.html.erb
[ADDED] spec/requests/helloworld_spec.rb
[ADDED] app/helpers/helloworld_helper.rb
Don't forget to add request/controller tests first.もう一回アプリケーションを起動して、作成したコントローラーにブラウザでアクセスしてみるとメッセージが表示されます。
$ merb
Loading init file from /home/jkikuchi/src/helloworld/config/init.rb
Loading /home/jkikuchi/src/helloworld/config/environments/development.rb
~ Connecting to database...
~ Loaded slice 'MerbAuthSlicePassword' ...
~ Parent pid: 15881
~ Compiling routes...
~ Activating slice 'MerbAuthSlicePassword' ...
merb : worker (port 4000) ~ Starting Mongrel at port 4000
merb : worker (port 4000) ~ Successfully bound to port 4000
merb : worker (port 4000) ~ Started request handling: Mon Nov 24 03:03:49 +0900 2008
merb : worker (port 4000) ~ Routed to: {"format"=>nil, "action"=>"index", "id"=>nil, "controller"=>"helloworld"}
merb : worker (port 4000) ~ Params: {"format"=>nil, "action"=>"index", "id"=>nil, "controller"=>"helloworld"}
merb : worker (port 4000) ~ {:before_filters_time=>0.000119, :dispatch_time=>0.006517, :after_filters_time=>4.2e-05, :action_time=>0.004517}
merb : worker (port 4000) ~http://localhost:4000/helloworld
You're in index of the Helloworld controller.
ビュースクリプトを修正して、表示されるメッセージを変更します。
$ vim app/views/helloworld/index.html.erb Hello World!
もう一度、merb コマンドでアプリケーションを起動してブラウザで表示してみると、Hello World! と表示されます。
ブラウザに表示されたページの HTML ソースを見ると、このようになっています。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
<title>Fresh Merb App</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/stylesheets/master.css" type="text/css" media="screen" charset="utf-8" />
</head>
<body>
Hello World!
</body>
</html>app/views/helloworld/index.html.erb の内容が body タグの中に入っているのがわかります。
外側の html タグなどは rails と同様に app/views/layout/application.html.erb に記述されています。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
<title>Fresh Merb App</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/stylesheets/master.css" type="text/css" media="screen" charset="utf-8" />
</head>
<body>
<%#= message[:notice] %>
<%= catch_content :for_layout %>
</body>
</html>Merb - Ruby の WEB アプリケーションフレームワーク