Instant Rails データベースを用意

どのような名前のデータベースを用意すればよいのかは、 database.yml を確認。

rails_apps/todo_app/config/database.yml」
development:
 adapter: mysql
 encoding: utf8
 database: todo_app_development
 username: root
 password:
 host: localhost

database: todo_app_development
アプリケーションディレクトリ名に "_development" をつけた名前
これも Rails の規約。この名前でデータベースを作成すればよい

MySQL コマンドを使ってデータベースを作る

1.todo_app> mysql -u root
 
C:\InstantRails20\rails_apps\todo_app>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.27-community
 
Type 'help;' or '\h' for help. Type2. '\c' to clear the buffer.
 
 
 2.mysql> create database todo_app_development;
 
mysql> create database todo_app_development;
Query OK, 1 row affected (0.19 sec)
 
 
 参考.quit (MySQL からログアウト)
 
mysql> quit
Bye

Rails ではモデルを生成する仕組みを使って、データベースに用意するテーブルを定義
script ディレクトリにある generate スクリプトを使い、モデルの元となるコードと
関連するテーブルをデータベース上に作成するプログラムのスケルトンを生成する

Rails の規約に従い、モデルには最初が大文字で単数形の名前をつけ
テーブルには小文字で複数形の名前をつける

todo_app>ruby script/generate model Todo
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/todo.rb
create test/unit/todo_test.rb
create test/fixtures/todos.yml
create db/migrate
create db/migrate/001_create_todos.rb

モデルファイル(todo_apps\models\todo.rb)
テーブル作成用コード(todo_apps\db\migrate\001_create_todos.rb)の確認

「models/todo.rb」
class Todo < ActiveRecord::Base
end

「migrate/001_create_todos.rb」
class CreateTodos < ActiveRecord::Migration
 def self.up
  create_table :todos do |t|
 
   t.timestamps
  end
 end
 
 def self.down
  drop_table :todos
 end
end

create_table :todos do |t| からテーブルを作成するコード
ここにテーブルに必要なカラムを作成する記述を追加

「migrate/001_create_todos.rb」
class CreateTodos < ActiveRecord::Migration
 def self.up
  create_table :todos do |t|
   t.column :item, :string         #To Do アイテム
   t.column :name, :string         #To Do を登録した人の名前
   t.column :memo, :text         #その To Do に関係するメモ
   t.column :created_at, :timestamp   #その To Do の作成日付
  end
 end
 
 def self.down
  drop_table :todos
 end
end

コードを使って、データベースにテーブル作成(rake db:migrate)

todo_app>rake db:migrate
(in C:/InstantRails20/rails_apps/todo_app)
== 1 CreateTodos: migrating ===================================================
-- create_table(:todos)
-> 0.2810s
== 1 CreateTodos: migrated (0.2810s) ==========================================

データベースの確認(use コマンド、show コマンド)

mysql> use todo_app_development;
Database changed
mysql> show fields from todos;

Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
item varchar(255) YES NULL
name varchar(255) YES NULL
memo text YES NULL
created_at datetime YES NULL

5 rows in set (0.00 sec)

id カラムが自動的に定義される
これは、Rails の規約に従ってユニークなレコード識別子を各レコードにつける
id は重要なので、変更・削除しないように

Instant Rails アプリケーションの構造

  1. todo_app のディレクトリ構成を確認

Rails は、アプリケーション開発に必要なディレクトリ構成を規約として決めている
このため、設定ファイルを用意しなくても Convention over Configuration (CoC) の原則
に従ってコードの生成やアプリケーションの開発ができる

Instant Rails 初 Rails アプリケーション

  1. Instant Rails 起動
  2. Manage Rails Application 起動
    • I => Rails Application => Manage Rails Application をクリック
  3. Create New Rails App...
    • Manage Rails Application の Create New Rails App... をクリック
  4. 枠組み作成
  5. 作ったアプリケーションを起動
  6. ブラウザで動作確認

Instant Rails インストール

  1. InstantRailsWiki にアクセス
  2. Instant Rails ダウンロードページに遷移
  3. InstantRails-X.X-win.zip をクリック
  4. 適当なディレクトリに展開(今回は C:\)
  5. InstantRails.exe アイコンをダブルクリック
  6. はじめて起動した場合、こんなダイアログが表示されますが、「OK」をクリック
  7. Apache に対応するファイアーフォールブロックの確認。「ブロックを解除する」をクリック
  8. Instant Rails Manager ウィンドウ表示

Railsの起動

Railsでリクエスト処理のためのプロセスを起動する際
最初に行われることの1つは、config/environment.rb のロード
environment.rb のロードは、public/dispatch.rb に記述あり

「public/dispatch.rb」
require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)

Rails環境全体を必要とするほかのプロセスでも config/environment.rb が必要

  1. デフォルト環境設定 => http://d.hatena.ne.jp/e-coding/20091007
  2. ブートストラップ => http://d.hatena.ne.jp/e-coding/20091008
      参考: edge Rails => http://d.hatena.ne.jp/e-coding/20091009
  3. RubyGems => http://d.hatena.ne.jp/e-coding/20091010
  4. イニシャライズ => http://d.hatena.ne.jp/e-coding/20091011
  5. デフォルトのロードパス
      initializer.rb 参照 => http://d.hatena.ne.jp/e-coding/20091012
  6. Rails、モジュール、自動ロード => http://d.hatena.ne.jp/e-coding/20091013
  7. ビルドイン情報 => http://d.hatena.ne.jp/e-coding/20091014
  8. コンフィグレーション => http://d.hatena.ne.jp/e-coding/20091015
  9. 追加のコンフィグレーション => http://d.hatena.ne.jp/e-coding/20091016

development モード

development は Rails のデフォルトモードで、開発者がほとんどの時間を費やすところ

  1. クラスの自動的なリロード => http://d.hatena.ne.jp/e-coding/20091017
  2. Railsクラスローダー => http://d.hatena.ne.jp/e-coding/20091018

## config/environment.rb の設定より優先される
## development 環境では、アプリケーションのコードはリクエストの度にリロードされる
 
# Settings specified here will take precedence over those in config/environment.rb
 
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false

## 誤って nil でメソッドを呼び出したときにエラーメッセージを記録
## ブレイクポイントサーバーを有効
## エラーレポート全体を表示、キャッシュは無効
## メーラーが送信できなくても気にしない
 
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
 
# Enable the breakpoint server that script/breakpointer connects to
config.breakpoint_server = true
 
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_view.cache_template_extensions = false
config.action_view.debug_rjs = true
 
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false

test モード

ほとんどの場合 test 環境の設定を変更する必要がなさそう

## config/environment.rb の設定より優先される
## test 環境はアプリケーションのテストスイートを実行するためだけに使用
## それ以外の目的で操作する必要はない
## test データベースはテストスイートの"スクラッチスペース"
## テストを実行する度に一掃され、再作成されることに注意
## test データベースのデータをあてにしてはならない

# Settings specified here will take precedence over those in config/environment.rb
 
# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

## 誤って nil でメソッドを呼び出したときにエラーメッセージを記録
## エラーレポート全体を表示、キャッシュは無効
## ActionMailer にメールを送信させない
## :test 送信メソッドは送信メールを ActionMailer::Base.deliveries 配列に蓄積
 
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
 
# Show full error reports and disable caching
config.action_controller.consider_all_requests_local = true
config.action_controller.perform_caching = false
 
# Tell ActionMailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test