金子邦彦研究室プログラミングRuby による Web/データベース・プログラミングRuby on Rails バージョン 3 でのリクエスト URL,ルーティング定義,URL のパラメータ

Ruby on Rails バージョン 3 でのリクエスト URL,ルーティング定義,URL のパラメータ

Ruby on Rails バージョン 3 を使ってみる.

◆ 事前準備

ソフトウェアのインストール

  1. Ruby 処理系のインストールと, RubyGems のインストールと,Rails のインストールが済んでいること

    ※ この Web ページの手順をなぞる場合は,sqlite3 パッケージのインストールも済んでいること

  2. JRuby を使う場合に限り,Windows での Java JDK 18 (Java SE Development Kit 18) のインストールが済んでいること.

    前もって決めておく事項

    Rails アプリケーションのディレクトリと基本ファイルの生成,Rails アプリケーションのひな形 (scaffold アプリケーション) の生成,マイグレーション・ファイルでのテーブル定義,リレーショナルデータベースの生成

    1. 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
      

      [image]

      [image]

      Rails で使えるデータ型は次の通り

      • :binary
      • :boolean
      • :date
      • :datetime
      • :decimal
      • :float
      • :integer
      • :primary_key
      • :string
      • :text
      • :time
      • :timestamp
    2. (オプション) 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
      
    3. リレーショナルデータベースの作成

      rake db:create:all
      rake db:migrate
      

      [image]

    scaffold アプリケーションでのルーティング

    「ルーティング」とは, 「HTTP メソッド」と「所定のルート定義にマッチするリクエストURL」との対を, コントローラ/アクション・メソッドにマッピングすることの意味として説明を行う.

    scaffold アプリケーションでのマッピングと変数 params

    1. リクエスト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

      [image]
    2. リクエスト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

      [image]