データ一覧の表示
それでは、playersデータの一覧をCurlの高機能なRecordGridを使って表示してみましょう。Curl側のプログラムはJava開発者のためのCurl入門-クライアントサイドCurlとサーバサイドJavaの通信のコードを少し変更したものを使います。
Curlコード用ページ
Curlのリッチクライアントアプリの場合、ページの制御はすべてクライアント側のプログラムが行いますので、最初にCurlのプログラムの書かれているページを表示する必要があります。
app/view/players/start.html.erb
まず、このCurlコード用ページを作りましょう。テンプレートは app/view/players/start.html.erb にします。ここにCurl側の一覧表示プログラムを書きます。
{curl 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}
{import * from CURL.IO.JSON}
{let players: RecordSet =
{RecordSet
{RecordFields
{RecordField "id", caption = "id", domain = int} ,
{RecordField "name", caption = "name", domain = String} ,
{RecordField "team", caption = "team", domain = String} ,
{RecordField "no", caption = "no", domain = int} ,
{RecordField "goal", caption = "goal", domain = int} ,
{RecordField "assist", caption = "assist", domain = int}
}
}
}
{let server_url = "http://localhost:3000/"}
{define-proc public {add-rec
id: int,
name: String,
team: String,
no: int,
goal: int,
assist: int
}: void
let new-rec: Record = {players.new-record}
{new-rec.set "id", id}
{new-rec.set "name", name}
{new-rec.set "team", team}
{new-rec.set "no", no}
{new-rec.set "goal", goal}
{new-rec.set "assist", assist}
{players.append new-rec}
}
{define-proc public {get-plyaers-list}:void
{let playersJsonValue: JsonValue =
{JsonValue-parse
{url server_url & "players.json"}
}
}
{players.delete-all}
{for player: JsonObject in playersJsonValue do
let found?: bool = false
let id: int = 0
let name: String = {String}
let team: String = {String}
let no: int = 0
let goal: int = 0
let assist: int = 0
set (id, found?) = {player.get-if-exists "id"}
set (name, found?) = {player.get-if-exists "name"}
set (team, found?) = {player.get-if-exists "team"}
set (no, found?) = {player.get-if-exists "no"}
set (goal, found?) = {player.get-if-exists "goal"}
set (assist, found?) = {player.get-if-exists "assist"}
{if found? then
{add-rec id, name, team, no, goal, assist}
}
}
}
{let data_list: RecordGrid =
{RecordGrid
width = 16cm,
height = 6cm,
record-source = players,
{RecordGridColumn "id", width = 0},
{RecordGridColumn "name", width = 5cm},
{RecordGridColumn "team", width = 2cm},
{RecordGridColumn "no", width = 2cm},
{RecordGridColumn "goal", width = 3cm, halign = "right"},
{RecordGridColumn "assist", width = 3cm, halign = "right"}
}
}
{get-plyaers-list}
{value
{spaced-vbox
data_list
}
}
app/controllers/players_controller.rb
このテンプレートを表示するためのメソッドをコントロラーに追加します。Curlのコードを表示するので、前回のchart表示メソッドと似たコードになります。
class PlayersController < ApplicationController
def start
render :layout => false, :content_type => 'text/vnd.curl'
end
・・・ 以下省略 ・・・
config/route.rb
前回同様にURLとControllerの対応を記述したroute.rbの定義にstartメソッドを追加します。
ActionController::Routing::Routes.draw do |map|
map.resources :players, :collection => { :chart => :get, :start => :get }
・・・ 以下省略 ・・・
ここでサーバを再起動し、http://localhost:3000/players/startをアクセスすると以下のようなデータ一覧が表示されます。
トップページ
Ruby on Railsではトップページ(http://localhost:3000 /)がアクセスされた場合、public/index.htmlファイルが表示ますが、これもconfig/route.rbファイルで設定できます、今回は「http://localhost:3000/players/start」をトップページにしましょう。
このとき、public/index.htmlファイルを削除する必要があります。
ActionController::Routing::Routes.draw do |map|
map.resources :players, :collection => { :chart => :get, :start => :get }
map.root :controller => "players", :action => "start" # ← 追加
・・・ 以下省略 ・・・


