interaction design

Tools: Technologies

Creating an application

To try out Ruby on Rails you will build a simple address book application. The database will have only one table with the following fields: name, address, suburb, post_code, phone_number and email_address.

Start InstantRails

Set the PATH

  1. Before you do anything start InstantRails (InstantRails.exe) from the root InstantRails directory.
  2. In the InstantRails control panel Stop Apache.
  3. Open a terminal window (i.e. command line), and navigate to the InstantRails root directory.
  4. Then run the use_ruby batch file to set the PATH.

Create the database in MySQLAdmin

  1. In the Terminal window type:
    mysqladmin –u root create addressbook
  2. The database has now been created in MySQL. It is a database without tables at this point.
  3. After we create the application we’ll create the tables with Migrations.

Create the Rails application

  1. Return to the terminal window, and make sure the current directory is InstantRails/rails_apps.   We will create our application inside the rails_apps directory.
  2. Call the address book application “myaddressbook”.
  3. At the command prompt type:
    rails myaddressbook
  4. InstantRails now creates the directory structure for your application.
    Show me
  5. Check the newly created directory structure. It should look something like the following. Note the directory structure:
    Show me
  6. Expand the app directory, and notice the classic Model, View, Layout design.
  7. Now tell the application which database to use.  Select the config directory and open the file called database.yml
    Show me
  8. database.yml looks something like this
    Show me
  9. We are in “development” mode, so we want the section under development: We need to tell Rails what the name of our database is.

    Replace:

    database: myaddressbook_development

    with

    database: myaddressbook

Create the model

  1. Now go back to the command prompt. Make sure the current directory is
    rails_apps\myaddressbook
  2. Type: ruby script/generate model Address
    Our database table (which we create shortly) will be named addresses.
  3. It’s a Rails convention to name the model in the singular.  Rails will map the Address model onto the addresses table.
    Show me
  4. Rails creates several files. The two to note are address.rb and 001_create_addressed.rb

Use Rails migration to create the database table

  1. Open 001_create_addressed.rb in the db/migrate directory
    Show me
  2. Here’s the first piece of Rails code – written in Ruby
    Show me
  3. This is a migration class.  Here we create the table and its column names (or fields).  This method of creating tables and fields works on the principle that if something goes wrong we can wind back the database to an earlier version. This can save you a lot of hassle if you stuff up the database design.
    • The # symbol at the beginning of a line is a comment which ignored in the processing.
    • Notice the self.up method and the self.down method.  When we actually invoke the migration, Rails will create a table as specified in the self.up method.  You don’t have to interact with MySQL directly. More about migrations later in this unit. Let’s get on with it.
    • Edit the document so it looks like the following, and save it, and close it.  Note the column types (data types) for each field is simply “string”.  Other data types like Integer, date and Blob are also available, but for the sake of this exercise just use “string” for now.
      Show me

Create table, controller & scaffold

Use Rake to create the table

  1. Now we return to the command prompt and use a program called Rake to create the table and fields.
  2. At the command prompt type:
    rake db:migrate
  3. The table and fields have now been created in MySQL. The model is complete.

Create the controller

  1. We are nearly complete, but must create the controller before we can proceed any further we must create the controller.
  2. return to the command prompt if you are not already there, and type:
    ruby script/generate controller addressbook
    Show me
  3. Rails creates several files but the one of interest is
    addressbook_controller.rb

Create the scaffold

  1. Open addressbook_controller.rb
    Show me
  2. This is the second piece of Ruby code you have seen
    Show me
  3. Now apply some Rails magic by using a scaffold.  Tell Rails that you want to use the Address model for this controller. Rails looks after the rest.
  4. Edit the file so it looks like this, then save it:
    Show me
  5. The process is complete.
    All you need to do now is to start the application in the InstantRails control panel.
    Point your browser to this address:
    http://localhost:3000/addressbook/list
    Show me
  6. Troubleshooting: If the application fails, first check that the port number is the one assigned to the web server.
    Show me
  7. You should now test the application by Creating new address records, Reading existing ones, Updating them, and destroying them.
    You have created an online web application.
    Show me