Ruby on Rails バージョン 3 を使ってみる.
※ この Web ページの手順をなぞる場合は,sqlite3 パッケージのインストールも済んでいること
mysql, postgresql, sqlite3, oracle などが指定できる.
このページでは,次のテーブル定義を使う.
◆ SQLite 3 の場合:
create table order_records ( id INTEGER PRIMARY KEY autoincrement not null, year INTEGER not null, month INTEGER not null, day INTEGER not null, customer_name TEXT not null, product_name TEXT not null, unit_price REAL not null, qty INTEGER not null DEFAULT 1, created_at DATETIME not null, updated_at DATETIME );
◆ SQLite 3以外 の場合:
create table order_records ( id INTEGER PRIMARY KEY autoincrement not null, year INTEGER not null CHECK ( year > 2008 ), month INTEGER not null CHECK ( month >= 1 AND month <= 12 ), day INTEGER not null CHECK ( day >= 1 AND day <= 31 ), customer_name TEXT not null, product_name TEXT not null, unit_price REAL not null CHECK ( unit_price > 0 ), qty INTEGER not null DEFAULT 1 CHECK ( qty > 0 ), created_at DATETIME not null, updated_at DATETIME, CHECK ( ( unit_price * qty ) < 200000 ) );
cd <全体のルートディレクトリ> rails new hoge --database sqlite3 cd hoge rails generate scaffold order_record id:primary_key year:integer month:integer day:integer customer_name:text product_name:text unit_price:float qty:integer created_at:timestamp updated_at:timestamp
Rails で使えるデータ型は次の通り
マイグレーション・ファイルのファイル名は、 cc/migrate/20110901074423_create_order_records.rb のようになっています. 20110901074423 は実際の日付に読み替えてください.テーブル定義が記述されている.
◆ SQLite 3 でのマイグレーション・ファイルの設定例:
class CreateOrderRecords < ActiveRecord::Migration def change create_table :order_records do |t| t.integer :id, :null => false t.integer :year, :null => false t.integer :month, :null => false t.integer :day, :null => false t.text :customer_name, :null => false t.text :product_name, :null => false t.float :unit_price, :null => false t.integer :qty, :null => false, :default => 1 t.timestamp :created_at, :null => false t.timestamp :updated_at t.timestamps end end end
◆ SQLite 3 以外でのマイグレーション・ファイルの設定例:
http://guides.rails.info/migrations.html の記述によれば, マイグレーション・ファイル内に execute ... を含めることになる.実例を下に載せています.
SQLite 3 には ADD CONSTRAINT の機能が実装されていないため、下のプログラムは動かない(2011/09/01 時点).
class CreateOrderRecords < ActiveRecord::Migration def change create_table :order_records do |t| t.integer :id, :null => false t.integer :year, :null => false t.integer :month, :null => false t.integer :day, :null => false t.text :customer_name, :null => false t.text :product_name, :null => false t.float :unit_price, :null => false t.integer :qty, :null => false, :default => 1 t.timestamp :created_at, :null => false t.timestamp :updated_at t.timestamps end #add constraints execute "ALTER TABLE order_records ADD CONSTRAINT c1_order_records CHECK ( year > 2008 );" execute "ALTER TABLE order_records ADD CONSTRAINT c2_order_records CHECK ( month >= 1 AND month <= 12 );" execute "ALTER TABLE order_records ADD CONSTRAINT c3_order_records CHECK ( day >= 1 AND day <= 31 );" execute "ALTER TABLE order_records ADD CONSTRAINT c4_order_records CHECK ( unit_price > 0 );" execute "ALTER TABLE order_records ADD CONSTRAINT c5_order_records CHECK ( qty > 0 );" execute "ALTER TABLE order_records ADD CONSTRAINT c6_order_records CHECK ( ( unit_price * qty ) < 200000 );" end end
rake db:create:all rake db:migrate
「ルーティング」とは, 「HTTP メソッド」と「所定のルート定義にマッチするリクエストURL」との対を, コントローラ/アクション・メソッドにマッピングすることの意味として説明を行う.
実はルーティング定義ファイル config/routes.rb には「resources :order_records」という記述しかない。この記述により、上記のひとそろいのルーティング定義を行ったことになる.
rails server
http://127.0.0.1:3000/order_records
http://127.0.0.1:3000/order_records/new
※ 下の実行例では、すでに id = 1 のオブジェクトを生成済み
http://127.0.0.1:3000/order_records/1/edit
※ 下の実行例では、すでに id = 1 のオブジェクトを生成済み
http://127.0.0.1:3000/order_records/1
例えば,次のようなルートを追加したいとする
GET /order_records/:id/foo(.:format) {:controller=>"order_records", :action=>"foo"}
match '/order_records/:id/foo(.:format)' => 'order_records#foo', :via => 'get'
ルートの追加ができていることを確認する.
http://127.0.0.1:3000/order_records/1/foo
ルートの追加はできたけど、アクション・メソッドがまだないので、次のように「Unknown action」と表示されたら成功
cp app/views/order_records/edit.html.erb app/views/order_records/foo.html.erb
コントローラー app/controllers/order_records_controller.rb を次のように書き換えてみる
# GET /order_records/1/foo # GET /order_records/1/foo.json def foo @order_record = OrderRecord.find(params[:id]) respond_to do |format| p format format.html # foo.html.erb format.json { head :ok } end end
◆エディタの画面
http://127.0.0.1:3000/order_records/1/foo
リクエスト URL http://127.0.0.1:3000/order_records/1/foo内の「1」を取り出したい.
ルーティングを「match '/order_records/:id/foo(.:format)' => 'order_records#foo', :via => 'get'」のように定義したので、1という値は, パラメータ id の値になる
コントローラー内で、パラメータ id の値を取得するには params[:id]のように書く. そこで、 app/controllers/order_records_controller.rb を次のように書き換えてみる
# GET /order_records/1/foo # GET /order_records/1/foo.json def foo @id=params[:id] @order_record = OrderRecord.find(params[:id]) respond_to do |format| format.html { render :text => "id = #{@id}" } format.json { head :ok } end end
Web ブラウザで、リクエスト URL http://127.0.0.1:3000/order_records/1/foo
# GET /order_records/1/foo # GET /order_records/1/foo.json def foo @id=params[:id] @hoge=params[:hoge] @format=params[:format] @order_record = OrderRecord.find(params[:id]) respond_to do |format| format.html { render :text => "id = #{@id}, hoge=#{@hoge}, params=#{params}" } format.json { head :ok } end end
Web ブラウザで、リクエスト URL http://127.0.0.1:3000/order_records/1/foo