RoR First Database App

Now that we have installed Rails and written our Hello from Rails app, let’s move on to a database backed application.

Create a Database

The first thing we need to do is to create a MySQL database. You can do this with the SQLyog tool. Start SQLyog and connect (as setup in RoR Setup). Then select:

DB -> Create Database -> "demo"

Edit database.yml

Now edit the config/database.yml file so the development section looks like:

development:
  adapter: mysql
  database: demo
  username: root
  password: mypassword
  host: localhost

To test that the database connection is working, open a command window and type:

c:\rails\demo>rake db:migrate
  (in c:/rails/demo)

If any error messages appear, debug the problem before continuing.

Setup Tables

Now let’s setup the table data. We will do this using the Rails migration facility.

First, create the migration template:

c:\rails\demo>ruby script\generate migration Initial
    create  db/migrate
    create  db/migrate/001_initial.rb

Then edit the file db\migrate\001_initial.rb so it looks like this:

class Initial < ActiveRecord::Migration
  def self.up
    create_table "registrants", :force => true do |t|
      t.column "firstname", :string, :limit => 50
      t.column "lastname", :string, :limit => 50
      t.column "email", :string, :limit => 60, :default => "", :null => false
      t.column "phone", :string, :limit => 20
      t.column "confirmed", :integer, :default => 0
    end  
  end

  def self.down
  end
end

Now go back to your command prompt and type:

  c:\rails\demo>rake db:migrate
    (in c:/rails/demo)
     Initial: migrating =================================
   —create_table(“registrants”, {:force=>true})
       -> 0.7810s
     Initial: migrated (0.7810s) =========================

That’s it! Your table is setup. If you would like to see it, go back to SQLyog and do a refresh.

Setup Scaffold

You are now ready to setup the scaffolding. Let’s use the ajax scaffold because it is cooler than the regular scaffold. Get the ajax scaffold by typing:

c:\rails\demo>gem install ajax_scaffold_generator
  Successfully installed ajax_scaffold_generator-3.1.5

Now use this ajax scaffold like this:

c:\rails\demo>ruby script\generate ajax_scaffold registrant
    exists  app/controllers
exists app/helpers
create app/views/registrants exists public/images exists test/functional
dependency model exists app/models
... create public/javascripts/ajax_scaffold.js create public/javascripts/rico_corner.js create public/images/indicator.gif create public/images/add.gif create public/images/error.gif

(Note1: “registrant” is singular in the above line.)

(Note2: To use the regular scaffold, type the command “ruby script\generate scaffold registrant”.)

Enjoy!

If Webrick is still running, go to that window and type Ctrl-C to stop it. Then restart so that the new files are picked up. Then click on this URL:

http://localhost:3000/registrants

And you are up and running! Click the “Create New” button in the upper-right corner to add a new registrant and then try the Edit and Delete buttons. Could getting a database-backed web site up and running be any easier?

To Discuss

  • Layouts
  • Render partial
  • Rails ORM (See registrants_controller.rb, edit and update)
  • Ajax