Rails スタート地点
初めてRailsを触る時からメモ帳っぽいものを作るところまで。
下準備
データベース
- PostgreSQL8.2.5をダウンロード&インストール。
- binにPathを通す。(RadRailsが起動済みの場合は上げなおす)
ruby-postgres
- GEMで入れる。
DOS窓>gem install ruby-postgres Select which gem to install for your platform (i386-mswin32) 1. ruby-postgres 0.7.1.2006.04.06 (ruby) 2. ruby-postgres 0.7.1.2006.04.06 (mswin32) 3. ruby-postgres 0.7.1.2006.04.05 (mswin32) 4. ruby-postgres 0.7.1.2006.04.05 (ruby) 5. Skip this gem 6. Cancel installation > 2 Successfully installed ruby-postgres-0.7.1.2006.04.06-mswin32 Installing ri documentation for ruby-postgres-0.7.1.2006.04.06-mswin32... Installing RDoc documentation for ruby-postgres-0.7.1.2006.04.06-mswin32...
RadRails
プロジェクト作成
Rails Projectを選択する。ここではプロジェクト名「message」で作成。
DB接続設定
\message\config\database.yml
development: adapter: postgresql database: message_development username: DBロール名 password: DBパスワード host: localhost
DB作成
PostgreSQLにDB名「message_development」を作成する。文字コードはUTF-8。
GUIに慣れている人はpgAdminを使うと楽。
modelをgenerate
画面下部のGeneratorsタブ
- Model
- message
- 「GO」をクリック
DBテーブル定義
\message\db\migrate\001_create_messages.rb
class CreateMessages < ActiveRecord::Migration def self.up create_table :messages do |t| t.column :name, :string, :limit =>32 t.column :title, :string, :limit =>128 t.column :created_at, :timestamp t.column :memo, :text end end def self.down drop_table :messages end end
マイグレート
画面下部のRake Tasksタブ
- db:migrate
- (空欄)
- 「GO」をクリック
(in D:/Aptana/message) == CreateMessages: migrating ============================== ==================== -- create_table(:messages) -> 0.2350s == CreateMessages: migrated (0.2350s) ===================== ==================== NOTICE: CREATE TABLE will create implicit sequence "messages_id_seq" for serial column "messages.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "messages_pkey" for table "messages"
テーブル完成。
スキャフォールド
画面下部のGeneratorsタブ
- scaffold
- message
- 「GO」をクリック
- Serverタブで動作確認
http://localhost:3000/messages/
ポート番号は違うかも。