Friday, October 23, 2009

Ruby-on-Rails Installation & Implementation

Objectives:
  • Install RoR and Gems
  • Setting up SVN
  • Develop Rails applications with painless deployment in mind;
  • Build and configure web servers and other services;
  • Stress application before deploying
Installing on local machine (windows xp)
First download and install ruby16-27_irc2.exe by double clicking on the exe file. (Installation steps are skipped). The next step is to update the installation using gem.

Updating Gem
Gem is the name of the utility supplied with Ruby in order to manage, install, and update the Ruby installation in an easy way. The second step in the manual installation of RoR is updating the Ruby installation so that if a new package or an update for any of the package is available, then the complete installation can be made up-to-date.

Before we begin, if you are behind a proxy, open the command prompt and give the following command:
>Set HTTP_PROXY=http://proxy_address:proxy_port

>gem update
That completes the update gem step. Next, let us install RoR.

Installing RoR
This is the last and the easiest part of the installation process. Just one command and RoR shall be installed.
At the prompt, issue the following command:
>gem install rails --include-dependencies

It would give the following message.
Congrats! RoR is now installed on your system.

Next we will update ruby and install necessary gems…
>gem update -system -rdoc
>gem install mysql

Testing the Installation
The installation is successful as much as the process is concerned. However, it is always a good idea to test the installation. From this point onwards, I will be using the rails_apps directory as the base directory for the RoR application that will be developed.

Creating a Starter Application
When you are testing a deployment configuration. You’ll want this simple application to do enough with Rails so you can see whether your production setup works. You don’t want to have to debug your deployment environment and your application at the same time. Once you have that working, you can move on to your real application.

You’ll want to do all the following steps on your development machine, not on your host. Your goal is to build a Rails application that exercises Rails models, views, and controllers. Since the default Rails project already tests the controller and views by default, you need to worry only about a primitive model.

Four steps to new application:
  1. Setting up the Application Structure
  2. Adding the First Controller class
  3. Implementing the Action Method
  4. Adding the View Template

Fire up command prompt and switch to rails_apps directory. The installation has to be tested for two components Ruby and Rails
>rails –d mysql dotevo

Step above will give you a project with mysql database backing. There will be lots of files generated. It means RoR installation is OK. What has happened is that RoR has generated the whole file structure for the application. Even certain files that work as placeholders have been generated by just one command.

Directories and Files Located in the directory
dotevo/
|-- README
|-- Rakefile
|-- app
|-- components
|-- config
|-- db
|-- doc
|-- lib
|-- log
|-- public
|-- script
|-- test
`-- vendor

A brief description of the directory structure and files is provided as follows:

Name .......... Description
README......... Gives a brief introduction to Rails and how to get started
Rakefile ...... the applications build script, which is written in Ruby
app ........... Contains your application’s code, including models, controllers, views and helpers
components .... Empty by default; reusable code in the form of components should be placed here.
Note that using components is generally considered a bad practice.
config ....... Holds your application’s configuration files, including database config.
db ............ Holds your ActiveRecord migrations and database schema files
doc ........... Empty by default; put the documentation for your application in this
directory (use rake appdoc to generate the documentation for your controllers and models)
lib ........... Holds application-specific code that is reusable
log ........... Log files are written to this directory
public ........ Contains static files like HTML, CSS, and JavaScript files
script ....... Contains various scripts for starting and maintaining your Rails application
test .......... Holds your unit, integration, and functional tests and their fixtures
vendor ........ Where plugins are installed

Now start the server with mongrel and start the browser to see our newly created project on http://localhost:3000

Check the server provided by RoR. Give the following command after changing into the dotevo folder.

> cd dotevo
> ruby script/server

As a response to the command, you should see the messages saying that it is booting up the Mongrel server since I had Mongrel installed on the system, the default is WEBrick…

Next, open the browser of your choice and provide the following URL:
http://localhost:3000.

The code you see on the Welcome page is in the index.html file, which is located in the public directory under the application root. It was created by the rails command and should be deleted, so that it doesn’t prevent the controller for the root context from being called:

C:\> del public\index.html

You will get an error, Recognition failed for "/", if you access http://localhost:3000 again after deleting index.html. The error is thrown because there are no controller and action set up to handle the request.

We are now ready to start writing some code.

First, we’ll see how requests are handled by the Rails framework. Later, we will modify the skeleton application that Ruby on Rails created for us to fit our requirements.

Configuring Ruby on Rails to Use the Database
The information Ruby on Rails needs for connecting to the database is located in a configuration file that is written in a lightweight markup language called YAML. The configuration file, database.yml, is located in the db folder and was created for you when you ran the rails command earlier. The generated configuration template contains examples for MySQL,

Database Configuration
# MySQL version
# gem install mysql

development:
adapter: mysql
encoding: utf8
reconnect: false
database: dotevo_development
pool: 5
username: myuser
password: mypwd
host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql
encoding: utf8
reconnect: false
database: dotevo_test
pool: 5
username: myuser
password: mypwd
host: localhost


Save the configuration after you are finished editing it.
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

Run the commands as follows:
> cd dotevo
> ruby script/generate model type_businesses
The last command will give you a model and also generated a migration that you can use to create your database-backed model.

The next step is to edit the migration so that it will create the type_businesses table structure. At this point, we can return to text editor to do our editing by opening the file db/migrate/20091002214424_create_type_businesses.rb

After making the changes it should be as follows:

class CreateTypeBusinesses < ActiveRecord::Migration
def self.up
create_table :type_businesses do |t|
t.column :type_description, :string, :limit => 45, :null => false
t.column :type_activity, :date, :null => false, :default => Time.now
end
end

def self.down
drop_table :type_businesses
end
end

Now use rake to migrate for generating table in our dotevo_developments database and verify if the table is generated properly as follows:
> rake db:migrate


Scaffolding:
To get some instant front-end rough view, we'll use another Rails feature called scaffolding. In order to generate a scaffold, you simply need to specify the name of the model you want to scaffold for with the column names and their data types. In our case, the model is called type_businesses. Therefore, from inside RAILS_ROOT, we will run on the database as follows:

> ruby script/generate scaffold type_businesses type_description:string type_activity:date

What happen in the above statement is that; we have generated a CRUD interface for our database table we created earlier in model with the name "type_businesses" the PK is automatically generated with column name "id", for other columns we have instructed to be "type_description" of data type string, and "type_activity" with data type date. All these columns are explicitly defined in our model which; we edited earlier.

***
And here it is a quick and dirty RoR test application, you can repeat the above steps for all the tables of your application and generate a raw front-end page using scaffolding for each table CRUD unit.
***