Rails tutorialをためす
MacBookAirで試す
% sw_vers ProductName: Mac OS X ProductVersion: 10.6.6 BuildVersion: 10J567 % ruby -v ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0] % gem -v 1.6.2 % rake --version rake, version 0.8.7
Installing Rails
公式サイトでは「たいていはroot権限でこのコマンドを実行」と記述されていますが、」
rvmを使っているのでgem installをrootでは実行してません
% gem install rails
Creating the Blog Application
% rails new blog % cd blog
Installing the Required Gems
% bundle install
Gemfileとbundle installの関係はperlでいうところのMakefile.PLとcpanm --installdeps .と似ている?
Creating the Database
すでにひな形が生成されているのでそのまま流用する。
一応中身をみておきましょう。
% cat config/database.yml
そうするとdevelopment, test, productionと設定がわかれています。
そもそも開発するんだから開発中と本番運用では設定ファイルの内容を切り替えなければいけないのですが、Ruby on Railsでは最初からひな形が作られています。
そしてdbを作成します。
% rake db:create
何やっているかは下記のとおり、config/database.ymlに従ってDBを作成しています。
% rake --task | grep 'db:create' [~/project/blog/lib/tasks] rake db:create # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
Starting up the Web Server
WEBrickを起動します。
% rails server [~/project/blog/lib/tasks]=> Booting WEBrick => Rails 3.0.5 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2011-03-27 13:03:06] INFO WEBrick 1.3.1 [2011-03-27 13:03:06] INFO ruby 1.9.2 (2011-02-18) [x86_64-darwin10.6.0] [2011-03-27 13:03:06] INFO WEBrick::HTTPServer#start: pid=6241 port=3000
screenか何かで別画面を開き、ブラウザで表示させます。
% open -a 'Google Chrome' 'http://localhost:3000'
WEBrickのプロセスはこんな情報を出力しています。
Started GET "/rails/info/properties" for 127.0.0.1 at 2011-03-27 13:07:48 +0900 Processing by Rails::InfoController#properties as SQL (0.7ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' SQL (0.2ms) SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence' SQL (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" Rendered inline template (0.7ms) Completed 200 OK in 51ms (Views: 2.0ms | ActiveRecord: 1.0ms)
で、ブラウザ上に表示されているAbout your application’s environment をクリックするとversionが表示されます。
Ruby version 1.9.2 (x86_64-darwin10.6.0) RubyGems version 1.6.2 Rack version 1.2 Rails version 3.0.5 Active Record version 3.0.5 Action Pack version 3.0.5 Active Resource version 3.0.5 Action Mailer version 3.0.5 Active Support version 3.0.5 Middleware ActionDispatch::Static Rack::Lock ActiveSupport::Cache::Strategy::LocalCache Rack::Runtime Rails::Rack::Logger ActionDispatch::ShowExceptions ActionDispatch::RemoteIp Rack::Sendfile ActionDispatch::Callbacks ActiveRecord::ConnectionAdapters::ConnectionManagement ActiveRecord::QueryCache ActionDispatch::Cookies ActionDispatch::Session::CookieStore ActionDispatch::Flash ActionDispatch::ParamsParser Rack::MethodOverride ActionDispatch::Head ActionDispatch::BestStandardsSupport Application root /Users/okamura/project/blog Environment development Database adapter sqlite3 Database schema version 0
Say “Hello”, Rails
ひな形を作成します。
% rails generate controller home index create app/controllers/home_controller.rb route get "home/index" invoke erb create app/views/home create app/views/home/index.html.erb invoke test_unit create test/functional/home_controller_test.rb invoke helper create app/helpers/home_helper.rb invoke test_unit create test/unit/helpers/home_helper_test.rb
いろいろなものが作成されます。
erbはテンプレート用の拡張子なんでしょう。きっと。
test/functional/*のファイルはWEBrickに対してリクエストを行い、意図した通りのレスポンスが帰ってくるかをテストする仕組みのようです。Contoller系のテストかな?
test/unit/helpers/*はmodel系のテストなの?
アクセスしてみる
% open -a 'Google Chrome' 'http://localhost:3000/home/index'
ひな形がそのまま表示されるのでテンプレートを修正する
% echo '<h1>Hello, Rails!</h1>' > app/views/home/index.html.erb
さきほどの画面を更新するとHello, Rails!と表示されます。
とりあえず今日はここま