Monday, September 7, 2009

Getting Started With Rails 2

Getting started with Rails 2.0 -A truly Agile Web Development
Rails is a web application framework that enables us to create dynamic, functional and architecturally  well designed websites with ease.

Getting Started:
In Ubuntu Linux, simply type:-
sudo apt-get install rails
Now accept the non standard packages (if there is a warning)

To create a rails application, simply go to the appropriate base folder and type
rails "Application Name"
This would generate all the basic files/folders needed for our application.Note that in Rails 2.0, we need to specify the database of our choice as the default database id sqllite3.To use any other database like MySql, we need to type
rails -D mysql "Application Name"
Make sure that you have the appropriate database application installed at this step. Now look at the database configuration at "Application Directory"/config/database.yml file. In my case, this shows the following data :

For now, lets only concentrate on the development section of the file.
Adapter means the library used to interact with rails. If you do not have the appropriate library, and try to use the database, you would face errors.In the database entry, name an appropriate database name(default or your own liking – whichever you prefer). Then specify username and password. Note that in development environment, we generally leave the password as blank (us lazy programmers :) ). In that case, you still need a space after password: . This is because the space acts as a delimiter (alongwith colon) between the name and value pairs in the file. Lastly, the socket refers to the file that mysql is to be connected from rails on the local computer. This is generated automatically, but you may want to change it if you have more than one distribution of mysql on your machine(In my case, this mysql is installed with lampp distribution).Now we need to create our database. For this, simply type :
This will create the development database.Alternatively, you can even create all the databases

Now before you get bored, lets jump into some serious development work. Keeping the agile manifesto in mind, we need to perform our iteration 0. In rails, this can be done by the scaffold utility. Again, be Very sure of the rails distribution that You are using and the Documentation/Book you are consulting as there is a lot of difference between Rails 1.x and 2.x families. In Rails 2.0 simply type the following command inside your application base directory:
ruby script/generate scaffold Movie title:string about:text url:string
This generates a lot of files (Models, Views and of course, Controllers) for the scaffold. Before commencing with anything, however, keep this in mind that a Scaffold is temporary (just like wooden struts that hold the roof of your house while the roof gets constructed) and should not be used for production code.

Note that the Scaffold name is Movie and it contains name:value pairs of data. You can even have a look into the database and we'll see a table created with name as prural form of our scaffold.Inside the table, rails auto generates a primary key as well as columns having creation and updation status of the records (all this without a single piece of sql/programmer intervention).

After doing this, simply start the server by running the following at command line :
ruby script/server start

This is our initial display of the application. Now play around with the data(perform crud operations to feel the power of this framework).
Note that using scaffold is the exception, not the norm while creating Rails application.

A page view of some newly entered data.
We can even perform validation in the form of models i.e. only the model needs to be refactored in order to perform this task. To do this, simply navigate to the /app/models directory in your rails application. Since in this example, I've created Movie as my model, there would be a movie.rb file in the model directory. It has the following code initially:
class Movie < ActiveRecord::Base


Clearly, the Model has class as Movie and it subclasses the Base class from ActiveRecord module. Inside this, simply insert the folowing piece of code (quite self explaining):
validates_presence_of :title,:about,:url
Now restart/refresh the web browser to observe the change. This time, try performing some validation errors to see the difference.

Thus we can observe the ease of use and the agility of the process involved in the development of a rails application.
Rails is a emerging framework, it is something that is 'bleeding edge' . For up to date information, do look for blogs and discussions on the web. In near future (by end of 2009), Merb framework is going to be integrated with Rails to form Rails 3. So be excited till then.
The following books are my prime sources in learning ruby on rails:

Beginning Ruby On Rails (Wrox)-Steven Holzner
Building Dynamic web 2.0 Websites with Ruby On Rails (Packt Publishing)-A.P. Rajshekhar
Agile Web Development with Rails Second edition (ThePragmatic Bookshelf)-Dave Thomas, David Heinemeier Hansson
Simply Rails 2 (SitePoint)-Patrick Lenz

No comments: