Getting a feel of Rails and TG
Posted On September 12, 2007 by Priyadarshan Roy filed under Internet
In the last article we peeped into the new world of web application frameworks. In this article we take a close look at two of them RubyonRails and Pythonic framework Turbogears.
RubyonRails(RoR)
For the uninitiated, Ruby is a beautiful, Object Oriented and Agile programming language that is dynamically typed. It is very similar to Python in many ways, but is more object oriented than Python. It is newer than Python and hence there is a lot more features. Ruby also draws inspiration from SmallTalk and from Perl. It originated in Japan, but is fast being accepted as potential winner in English speaking lands too.
Ruby as a language was nothing beyond academic interest till three years or so. In the past one year or so, Ruby has captured the imagination of people to an extent that it is believed to the fastest growing programming language as of today. And one of the biggest reasons in Rails
RubyonRails is a framework that was developed by David Hannson, a partner of the development company 37Signals.
To understand RubyonRails it is best to install it on your system and tryout for yourselves.
Getting ROR
You can find RubyonRails package files in the CDs along with the magazine. You can otherwise download it also from the net. If you want to just play around with ROR, then I would always suggest you to check out InstantRails for Windows. This is not included in this edition of the magazine, but will be seen in the March edition. This includes MySQL, Apache, Ruby and Rails.
In Linux which I feel is the natural choice of developing and deploying Rails application you have to run the following steps.
Check first whether you have Ruby already installed, and check out the version.
RoR 1.0 works with only Ruby 1.8.2 and Ruby 1.8.4. In case you do not have these then you need to have either of these installed.
You need Gems. Gems is an automatic package installer for Ruby. It can be compared with aptget or yum.
You need to download the tgz file, unzip it, un tar it and then run ruby setup.rb, to have gems installed.
Depending on the operating system Rails can be installed as follows
On Linux
gem install rails --include-dependencies
on Windows
gem install rails –remote
You need to then install MySQL, if it is not already installed. Windows users is advised to download the InstantRails to play round with this framework, till you decide that Rails is the way to go.
In rest of this article I will use the InstantRails on Windows as a platform to explain how Rails work. How fast you can write application in Ruby without knowing any Ruby at all. All you need to follow is the installation guidelines which is exactly what I did.
The steps are follows.
Download and unzip the Instant Rails zip file.
Make sure the installation path does not contain any space characters, and then start InstantRails.exe.
Instant Rails will detect that it is being started from a new directory and ask if you want to have it update the paths in the all of the configuration files... Just say yes. It will be automatically taken care.
The cookbook app is already configured to use SCGI on port 9999, and Typo is already configured to use SCGI on port 9998, but we must tell Windows to fake the domain names that are preconfigured in the Apache configuration file:
Click on the I button (or press and release the Alt key twice) to drop down the main menu and select Configure > Windows Hosts file. See figure 1.
In the editor that pops up, add these two lines to the end of the file:
127.0.0.1 www.mycookbook.com
127.0.0.1 typo
Save the changes to the file (you must have Administrator privileges to do this), the filename is 'hosts' with no extension. Exit the editor.
In the main menu, select Rails Applications > Manage Rails Applications...
Check the checkbox next to the cookbook application.
Click on the Start SCGI button. A minimized console window will appear. This contains the cookbook web app running through an SCGI server.
Wait a few seconds to make sure the cookbook web app is fully initialized (nothing is displayed in the console window). Open your browser and go to:
HTTP://www.mycookbook.com
You should now be using a running Rails application!
Running Typo
In the main menu, select Rails Applications > Manage Rails Applications...
Check the checkbox next to the typo application.
Click on the Start SCGI button. A minimized console window will appear. This contains the typo web app running through an SCGI server.
Wait a few seconds to make sure the Typo is fully initialized (nothing is displayed in the console window). Open your browser and go to:
HTTP://typo
You should be taken through a series of admin setup screens.
Checking out the guts of Rails
You can create a Rails application by clicking on the button with same name, in the Rails Application window. You will have a console window opened and a number of folders created as shown in figure 3.
Check the folder cookbook
You will see a directory structure as given in console window
| .............................................................................. D:\shah\InstantRails-1.0\rails_apps>cd cookbook D:\shah\InstantRails-1.0\rails_apps\cookbook>dir Volume in drive D has no label. Volume Serial Number is 648A-C876 Directory of D:\shah\InstantRails-1.0\rails_apps\cookbook 01/25/2006 10:12 AM <DIR> . 01/25/2006 10:12 AM <DIR> .. 01/25/2006 10:12 AM <DIR> app 12/06/2005 10:55 PM 34,992 CHANGELOG 01/25/2006 10:12 AM <DIR> config 01/25/2006 10:12 AM <DIR> db 01/25/2006 10:12 AM <DIR> doc 01/25/2006 11:08 AM <DIR> log 01/25/2006 10:12 AM <DIR> public 12/06/2005 10:55 PM 316 Rakefile 12/06/2005 10:55 PM 7,233 README 01/25/2006 10:12 AM <DIR> script 01/25/2006 10:12 AM <DIR> test 3 File(s) 42,541 bytes 10 Dir(s) 12,899,033,088 bytes free D:\shah\InstantRails-1.0\rails_apps\cookbook> |
Figure 4
Looking inside the app folder of cookbook, you will find directory structure as follows
| ................................................................................................................................................................ 01/25/2006 10:12 AM <DIR> . 01/25/2006 10:12 AM <DIR> .. 01/25/2006 10:12 AM <DIR> controllers 01/25/2006 10:12 AM <DIR> helpers 01/25/2006 10:12 AM <DIR> models 01/25/2006 10:12 AM <DIR> views 0 File(s) 0 bytes 6 Dir(s) 12,899,033,088 bytes free |
You will find a file titled ‘project-name’-start.py and in this case sample-start.py.
Just execute the file with the usual command
/python sample-start.py
Point your browser to the link http://localhost:8080. You will find a welcome screen.
Explore the folder you will see another folder called sample which contains two file model.py and controllers.py. In the case RoR, it was two folders. Further exploring each file you will find that model.py essentially is used to write the ORM, and controllers.py pulls all the information needed for the web server to print the pages. In TurboGears, your view is in templates that present the information that is provided by the controller. Inside the templates folder you will files with extension kid which will create the templates.
Essentially TurboGears is an inspired Pythonic attempt of emulating Rails. We will take this discussion further by attempting to write a similar application using Rails, TG and using LAMP in the next articles.
