Using Galatea Dialog Studio with web application

We can realize application which controls Galatea by cooperating with framework of web application.

Making of the project of Ruby on Rails

We can install Ruby on Rails which became Ubuntu package by the following procedure.

$ sudo apt-get install rails

We show example with Ruby on Rails 2.0.2 installed in gem.

$ which rails
$ rails dialog
      create  app/controllers
      create  app/helpers
      create  app/models

$ ./dialog/script/server
=> Booting WEBrick...
=> Rails application started on
=> Ctrl-C to shutdown server; call with --help for options
[2009-02-18 15:50:56] INFO  WEBrick 1.3.1
[2009-02-18 15:50:56] INFO  ruby 1.8.7 (2008-08-11) [i486-linux]
[2009-02-18 15:50:56] INFO  WEBrick::HTTPServer#start: pid=25026 port=3000

A screen of Rails is displayed when we open "http://localhost:3000/" with Mozilla Firefox.

Please stop script/server by pressing Ctrl-C.

We implement VoiceXML application of very simple Model-View-Controller cooperation.

At first we edit routes.rb so that View of the vxml format is referred to in a URL of the "http://server:port/model/index.vxml" format.

  • Please replace emacs with favorite screen editor.
  • Please use character code in UTF-8.
$ emacs dialog/config/routes.rb
ActionController::Routing::Routes.draw do |map|
  # ....
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action.:format' # add this

Making of Model

We make Model (database) by the name of product.

$ cd dialog
$ ./script/generate model product name:string yomi:string price:integer
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/product.rb
      create  test/unit/product_test.rb
      create  test/fixtures/products.yml
      create  db/migrate
      create  db/migrate/001_create_products.rb
$ emacs test/fixtures/products.yml
  name: 玉子丼
  yomi: たまごどん
  price: 550
  name: のり弁
  yomi: のりべん
  price: 580
  name: おにぎりセット
  yomi: おにぎりせっと
  price: 600
  name: のりメンタイ
  yomi: のりめんたい
  price: 600
$ rake db:migrate
(in /home/nishi/myprojct/dialog)
== 1 CreateProducts: migrating ================================================
-- create_table(:products)
   -> 0.0052s
== 1 CreateProducts: migrated (0.0053s) =======================================

$ rake db:fixtures:load

Making of View and Controller

$ ./script/generate controller product 
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/product
      exists  test/functional/
      create  app/controllers/product_controller.rb
      create  test/functional/product_controller_test.rb
      create  app/helpers/product_helper.rb
$ emacs app/controllers/product_controller.rb
  • Add two methods (def ... end) between the class and the end.
class ProductController < ApplicationController
  def index
    @products = Product.find(:all)
  def show
    @product = Product.find(params[:id])
$ emacs app/views/product/index.vxml.erb
<?xml version="1.0"?>
<vxml version="2.0" xml:lang="ja">
 <form id='main'>
  <field name='id'>
   <prompt timeout='20s'>
    <% @products.each do |p| -%>
    <%=h -%>、
    <% end -%>
   <grammar version='1.0' root='#product'>
    <rule id='product'>
    <% @products.each do |p| -%>
    <item> <token sym="<%=h p.yomi %>" slot="id" value="<%=h %>"> <%=h %> </token> </item>
    <% end -%>
    <item> <token sym="まいくてすと">マイクテスト</token> </item>
   <submit next="<%= url_for(:action=>'show', :format=>'vxml') %>"/>
$ emacs app/views/product/show.vxml.erb
<?xml version="1.0"?>
<vxml version="2.0" xml:lang="ja">
 <form id='main'>
    <%=h %>は<%=h @product.price %>円です。<break time="1s"/>    
  <goto next="<%= url_for(:action=>'index', :format=>'vxml') %>" />

The setup was done.

Starting rails server and galatea at the same time

The runner of galatea has a option to start rails application.

$ cd ..
$ ./script/runner --rails-server ./dialog http://localhost:3000/product/index.vxml