Ruby on Rails チュートリアルのメモ(2)

前より更に雑なメモ.

2章 デモアプリケーション

scaffoldジェネレータというスクリプトを使ってアプリケーションを生成.Railsアプリケーションの概要を掴む.

2.1 アプリの計画

railsコマンドでアプリケーションの骨格を生成
% cd rails_projects
% rails _4.0.5_ new demo_app
% cd demo_app

Bundlerで使用するGemfileをテキストエディタで編集
% vim Gemfile

本番用のgemを除いたローカルgemをインストール(--without productionオプション)
% bundle install --without production
% bundle update
% bundle install

バージョン管理下におく
% git init
% git add .
% git commit -m "Initial commit"

2.2 Usersリソース

ユーザー用のデータモデルを,そのモデルを表示するためのWebインターフェイスに従って実装.

scaffoldでコードを生成
% rails generate scaffold User name:string email:string

データベースをマイグレート
% bundle exec rake db:migrate

ローカルWebサーバーを起動
% rails s

2.3 Micropostsリソース

scaffoldでコードを生成
% rails generate scaffold Micropost content:string user_id:integer

マイグレーションを実行
% bundle exec rake db:migrate
  • マイクロポストの最大文字数を制限
  • ユーザーとマイクロポストをhas_manyで関連づける
  • 継承の階層
  • デモアプリケーションのデプロイ