Ruby on Rails バージョン 3 でのリクエスト URL,ルーティング定義,URL のパラメータ
Ruby on Rails バージョン 3 を使ってみる.
◆ 事前準備
ソフトウェアのインストール
- Ruby 処理系のインストールと, RubyGems のインストールと,Rails のインストールが済んでいること
* この Web ページの手順をなぞる場合は,sqlite3 パッケージのインストールも済んでいること
- JRuby を使う場合に限り,Windows での Java JDK 18 (Java SE Development Kit 18) のインストールが済んでいること.
前もって決めておく事項
- 全体のルートディレクトリ
- Rails アプリケーション名: このページでは hoge と書く
- Rails アプリケーションのルートディレクトリ: 全体のルートディレクトリ/hoge のようになる.
- データベース管理システム名: このページでは sqlite3 を使う.
mysql, postgresql, sqlite3, oracle などが指定できる.
- モデル名(単数形): このページでは order_record と書く
- テーブル名(複数形): このページでは order_records と書く
- テーブル定義:
このページでは,次のテーブル定義を使う.
◆ 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 ) );
Rails アプリケーションのディレクトリと基本ファイルの生成,Rails アプリケーションのひな形 (scaffold アプリケーション) の生成,マイグレーション・ファイルでのテーブル定義,リレーショナルデータベースの生成
- Rails アプリケーションのひな形 (scaffold アプリケーション) の生成
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 で使えるデータ型は次の通り
- :binary
- :boolean
- :date
- :datetime
- :decimal
- :float
- :integer
- :primary_key
- :string
- :text
- :time
- :timestamp
- (オプション) scaffold アプリケーションの生成において生成されたマイグレーション・ファイルでのテーブル定義と一貫性制約の記述
マイグレーション・ファイルのファイル名は、 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
scaffold アプリケーションでのルーティング
「ルーティング」とは, 「HTTP メソッド」と「所定のルート定義にマッチするリクエストURL」との対を, コントローラ/アクション・メソッドにマッピングすることの意味として説明を行う.
- 「rake routes」コマンドによるルーティング定義の確認
- ルーティング定義ファイル config/routes.rb の確認
実はルーティング定義ファイル config/routes.rb には「resources :order_records」という記述しかない。この記述により、上記のひとそろいのルーティング定義を行ったことになる.
- Rails サーバの起動
rails server
- HTTP メソッド GET に関するルーティングの確認
- GET /order_records の場合
http://127.0.0.1:3000/order_records
- GET /order_records/new の場合
http://127.0.0.1:3000/order_records/new
- GET /order_records/:id/edit の場合
* 下の実行例では、すでに id = 1 のオブジェクトを生成済み
http://127.0.0.1:3000/order_records/1/edit
- GET /order_records/:id の場合
* 下の実行例では、すでに id = 1 のオブジェクトを生成済み
http://127.0.0.1:3000/order_records/1
- GET /order_records の場合
- ルーティング定義への新しいルートの追加
例えば,次のようなルートを追加したいとする
GET /order_records/:id/foo(.:format) {:controller=>"order_records", :action=>"foo"}
-
ルーティング定義ファイル config/routes.rb に次の行を書き加える
match '/order_records/:id/foo(.:format)' => 'order_records#foo', :via => 'get'
- ルーティング定義ファイル config/routes.rb の確認
ルートの追加ができていることを確認する.
- Web ブラウザで次の URL を指定する
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
◆エディタの画面
- Web ブラウザで次の URL を指定する
http://127.0.0.1:3000/order_records/1/foo
-
ルーティング定義ファイル config/routes.rb に次の行を書き加える
scaffold アプリケーションでのマッピングと変数 params
- リクエストURL内のパラメータの取得
リクエスト 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
- リクエストURL内のクエリ形式のパラメータの取得
app/controllers/order_records_controller.rb を次のように書き換えてみる
# 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