• home
  • forum
  • my
  • kt
  • download
  • RUBY-ON-RAILS TUTORIAL USING INSTANT RAILS

    Author: 2008-08-29 09:51:38 From:

      Ruby-on-Rails is a hot topic now. Even,Netbeans is providing an IDE for developing RoR programs. RoR is said to make the task of developing web-based CRUD (create,read,update&delete) programs, very easy. Any modern web-based program has to provide the following features.

      i) MVC Architecture ( like Struts)

     ii) ORM feature     ( like Hibernate)

    iii) AJAX features   ( like GWTK)

     iv) In-built testing facilities

    --

       RoR provides all these features, readymade! I have written about Hibernate in this issue, in my j2me serial.I have used MySql database and a table named playertable in that example. I will use the same table schema for this RoR example also, so that we can appreciate the advantage that RoR offers.I am not going into the theory of ActiveRecords. I think that an analysis of the structure of RoR and ActiveRecords is a complex topic  and can wait.

    --

    I am using Instantrails 1.7 (the latest version is Instantrails-2.0.2). because it makes use of MySql database,by default,

    (Instantrails2.0.2 uses sqllite3. I am more familiar with MySql).

    InstantRails can be downloaded from its website. I unzipped  Instantrails1.7 into

    C:\unzipped_folder .The size of zip file is about 60MB , so it may take lot of time to get unzipped.After that, I renamed Instantrails1.7 as rails1.7.

    and placed it in g:\  drive.

    There is no installation as such.  It is simply a question of unzipping.I am using windows 2000.

    Navigate to g:\rails1.7>

    give the command 'Instantrails' as follows.

     g:\rails1.7>instantRails

     we will get the dialog.  as shown in

    screen-1.

    By default, we get Apache server and MySql running and ready. However, we will use Mongrel server in our demo. So stop the Apache server,by clicking on Apache button and using the stop option.. Now, click on the 'I' symbol in the dialog->Rails Application-> "Open ruby console" button.

    See screen-2. 

     We are taken to

    g:\rails1.7\rails_apps>

     

    Give the command "rails  players" as shown below

    g:\rails1.7\rails_apps>rails     players

    (players is the name of our project).

     

    Now go to InstantRails dialog and navigate to I->rails applications->manage rails application.You will get a display having a number of checkboxes ( showing the available projects).

    (Screen-3)

    ( cookbook and typo are the 2 demo projects which are already there). Check up whether checkbox with "players" is present.

    Select that checkbox as shown and click on "configure startup mode" for that project.

    (screen-4)

     

    .You are taken to configure dialog

    give port no as 3005 and click on' ok'. 

    (see screen-5)

     

    and click 'ok' at the bottom of the screen.

     

     

    Once again go to InstantRails dialog and click on I->rails applications->manage applications->"start with mongrel server"

    (screen-6)

    wait till starting is complete. Check up the mongrel server console. for the port number. It should be 3005 ( as specified by us).

     

    Now open the browser and type the url as "http://localhost:3005/"

    we will get welcome display for this project as shown ( screen-7)

     

    ==================================

    Now cd to g:\rails1.7\rails_apps

     

    ...\rails_apps>cd players

     

    ...\players>

    >ruby script/generate model  Player

     

    you will get series of messages.

     

     

    ( thus we have indicated that we want a table named 'players').But, we have to generate the schema. I will follow the easier method of creating the schema by SQL command. As we are using MySql database, we have to start it. For that, in the same console window ,give the command as follows.

     

    ...\players>mysql   -u     root

    (u stands for user)

     We will get  the mysql prompt

      mysql>

    Let us now create the database , and table.

    mysql>create database players_development;

     

    For each project, RoR creates three databases ( one each for development,testing and production).

    We are dealing now with 'development'.

    Our database is ready. We have to instruct to use this database.

    mysql>use players_development;

    Let us now create the players table with three columns ( id,name ,place).

     

    mysql>create table players(id int(3) auto_increment,name varchar(16),place varchar(16),primary key(id));

    ( type this in one line).

    Now, our table schema is ready. Just to check up, create a sample record.

     

    mysql> insert into players values('','ANAND','BOMBAY');

    (don't forget ";" in all sql commands)

    and check up by a query.

     

    mysql>select * from players;

    we get 1 ANAND BOMBAY

    ==========================

    Let us now add a few records by ORM principle.I have given a very brief note on what ORM means in my hibernate in j2me lesson in this issue.ORM will automatically persist objects in memory to corresponding row in a table of database in hard-disk,without our writing and executing any sql statement.

    We are currently in:

    g:\rails1.7\rails_apps\players>

     ..players>ruby script/console

     

    we get prompt as follows.

     

      >>

    The commands are as follows.

     >>player=Player.new

      we will get some response

    >> player.name="DAVID"

    >> player.place="DELHI"

    >>player.save

    we will get "true" message.

    >>quit

     

    Let us now  check up whether the above object created in memory has really been persisted in the specified mysql database.

     

    for that once again go to mysql as follows.

    ..players>mysql  -u root

      mysql> use players_development;

      (database changed)

     

    mysql> select * from players;

    we should get

     

    1 ANAND  BOMBAY

    2 DAVID  DELHI

     

    therefore we have verified that ORM is working correctly.

     

    -------------------------------------------

    Now comes the most intersting part, for creating web gui for CRUD operation.

     

    Give the command as follows

     

    ...\players>

    ruby script/generate controller  Players

    this will create number of folders and files.

     

    Look for

    G:\rails1.7\rails_apps\players\app\controllers\players_controller.rb

     

    After locating it, edit it as shown.

     

    class PlayersController <

                      ApplicationController

      scaffold   :player

    end

     

    Save it.

     

    Remember to shutdown  mongrel server.

    AND Restart the server for this project.

     

    Type the URL in browser as

    http://localhost:3005/players

     

    you will be surprised and happy to find that the two records already created by us are displayed on the screen.Also,  links for web-gui to add new record,edit record and remove record, have automatically been created.

    (screen-8)

     

    I need not know much about ruby language to make use of RubyonRails, especially when using Instantrails.This software bundles everything required to build an RoR project in a single set. (for windows platform). Everything is readymade. At the same time it incorparate MVC architecure like struts and ORM facility like Hibernate and even Ajax.

     

       If you go through my note on Hibernate, we had three steps there.

     a) hibernate.properties ( for mysql db)

     b) player.hbm.xml ( for mapping)

     c) player.java  ( for player class).

     

     Though, here also we have  a similar yaml file for MySql( created automatically), we did not create any player class nor any dbcolumn-attribute mapping xml file.

    The advantage becomes evident if there are about a dozen columns in the table.

    That is why R-O-R is called lightning-Fast Web Development by Bruce Tate.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Database Related (5)
      Getting Started (11)
      Helpers (5)
      Image Manipulation (2)
      Security (4)

    New

    Hot