Livecommunity powered by sixgroups.com
  ABOUT MASHUP CAMP WIKI BEST MASHUP CONTEST NEWS SPONSORS CONTACT TV BLOG WHO'S COMING?

RoR First Database App

From MashupCamp

Jump to: navigation, search

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

Contents

Create a Database

The first thing we need to do is to create a MySQL database. You can do this with the SQLyog tool. Add a new connection to "localhost", username "root" and the password you selected when you installed MySQL.

Then choose 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: my_password
   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)

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.

Setup Scaffold

You are now ready to setup the scaffolding. Do this by typing:

 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

(Note: "registrant" is singular in the above line.)

(Note2: if you get "couldnt find 'ajax_scaffold' generator" error.Use this work around - replace ajax_scaffold with scaffold in the command above and try again. So type "ruby script\generate scaffold registrant" instead of "ruby script\generate ajax_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 hit 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?