まずはプロジェクト作成

  • RadRailsを起動
  • Ruby エクスプローラー上で右クリック>新規>Rails プロジェクト
  • プロジェクト名に「ToDo」あとはデフォルトでOK。「プロジェクト作成後にサーバーを自動開始」にチェックを入れておくと自動実行されるのでチェックに便利。
  • 新規プロジェクト「ToDo」が作成される


疑問点:
マイグレーションとはなんぞや。自動生成の設定かな?
→「DBのスキーマ(テーブルやカラムなどの構造)の変更をツールで管理する仕組み」らしい。なるほど。

scaffoldでテーブル自動生成

  • ウィンドウ>ビューの表示>ジェネレーター を選択し、「ジェネレーター」ビューを画面に表示する
  • 生成プログラムに「scaffold」パラメーターに「Category category:string」を指定して右端の緑三角ボタンをクリック
script/generate scaffold Category category:string
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      exists  app/views/categories
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      exists  test/unit/helpers/
      exists  public/stylesheets/
      create  app/views/categories/index.html.erb
      create  app/views/categories/show.html.erb
      create  app/views/categories/new.html.erb
      create  app/views/categories/edit.html.erb
      create  app/views/layouts/categories.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/categories_controller.rb
      create  test/functional/categories_controller_test.rb
      create  app/helpers/categories_helper.rb
      create  test/unit/helpers/categories_helper_test.rb
       route  map.resources :categories
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/category.rb
      create    test/unit/category_test.rb
      create    test/fixtures/categories.yml
      create    db/migrate
      create    db/migrate/20100805021554_create_categories.rb
  • テーブル名:Category / 項目名:category / 型:string の条件で、マイグレーションファイル 「db/migrate/xxxxxxxxxxxxxx_create_categories.rb」が作成される
  • ウィンドウ>ビューの表示>Rakeタスク を選択し、「Rake タスク」をビュー画面に表示する
  • タスクに「db:migrate」を指定して右端の緑三角ボタンをクリック
rake db:migrate 
==  CreateCategories: migrating ===============================================
-- create_table(:categories)
   -> 0.0017s
==  CreateCategories: migrated (0.0019s) ======================================
  • 生成したdbは「db/development.sqlite3」の模様。「config/database.yml」の設定からすると、test.sqlite3とproduction.sqlite3もここにできるんだろうな。
  • ブラウザで「http://127.0.0.1:3000/categories」を開く →なんかできてる!カラム追加や削除など一通り試す。バリデートなどは全くないのでなんでも登録できる。空入力でも登録できる。システム内部処理時にRedRailsのコンソールへ色々ログ出力されるので便利。
  • テーブルが自動作成されると同時に、app/modelsの中にモデルも自動作成される。MVCのフォルダ構造はCakePHPと一緒なので、バリデートなどの設定をするならこちらをいじることになる。

CRUDとかRESTfulとかrouteとか

CakePHPと同様、RoRCRUD(Create, Read, Update, Delete)なディレクトリ構成でアクセスする。
RESTfulとはなんぞやについてはこちらを参照。もしくは資料のP.12〜13辺りに詳しい。

RoRの場合はrakeコマンドを使用することでアプリケーションのメソッドに対するHTTPメソッドの対応リストを見ることができる。
RedRailsの場合、

  • ウィンドウ>ビューの表示>Rakeタスク を選択し、「Rake タスク」をビュー画面に表示する
  • タスクに「routes」を指定して右端の緑三角ボタンをクリック

でコンソールにリスト表示される。

>rake routes 
   categories GET    /categories(.:format)              {:controller=>"categories", :action=>"index"}
              POST   /categories(.:format)              {:controller=>"categories", :action=>"create"}
 new_category GET    /categories/new(.:format)          {:controller=>"categories", :action=>"new"}
edit_category GET    /categories/:id/edit(.:format)     {:controller=>"categories", :action=>"edit"}
     category GET    /categories/:id(.:format)          {:controller=>"categories", :action=>"show"}
              PUT    /categories/:id(.:format)          {:controller=>"categories", :action=>"update"}
              DELETE /categories/:id(.:format)          {:controller=>"categories", :action=>"destroy"}
                     /:controller/:action/:id           
                     /:controller/:action/:id(.:format) 

うっほ超便利。