The Hitchhiker’s Guide to Ruby On Rails Galaxy

Records of my voyage through RoR Galaxy

How to Install Ruby, Rails 2.x, MySQL, SVN, then setup Rails 2.x Application, Configure the database, Run the Rake Tasks, Install plugins and, finally run RSpec for Models on your Local Windows System

Posted by arjunghosh on April 25, 2008

A.) How to setup a Rails 2.x Application on your local system:–
[NOTE: The following steps are for Windows OS environment]

  1. Setup Ruby and Rails on the local system:-
    • Install Ruby on Windows:
      The easiest way is using the One-Click-Installer
      Once the installation is complete, check that path to ruby\bin directory is in your PATH variable (Run “cmd”, then type “path” at the prompt and check that the path is there.
      The great thing about the one-click installer is that it comes with Ruby Gems, Scite pre-installed. The thing to watch out for is that one-click installer does not have the very latest version of Ruby, so when you are reading Ruby Docs, make sure you know which version of Ruby you have (run “ruby -v” in the command prompt)
    • Installing MySQL on Windows:
      First, download and run the latest MySQL. After the files are unzipped, an Instance Config should run automatically (you can run it manually at any time from your MySQL bin directory just run “MySQLInstanceConfig.exe”). Then go through the step as directed.
    • Install Rails on Windows:
      Run the following command: gem install rails –-include-dependencies
      (If this command gives you an error, you do not have the latest version of gems installed. Run “gem -v” to check for the version of RubyGems. It should be 0.9.4. Otherwise, you may probably need to update RubyGems as well.: gem update –system [NOTE: It is recommended that RubyGems version 0.9.4 is used as Rails 2.x had some issue with version 0.9.5. Though this issue had been subsequently resolved]
    • Installing SVN on Windows:
      We use the TortoiseSVN SVN client on our local machine to use SVN.
      We have SVN version on our local system: 1.4.3
      The SVN version installed on the Server, which we were using, can be found by running the command on the server: svn –version
  2. Create a rails 2.x application:-
    SQLite3 is the new default database. So when a rails 2.x application is created, by default the database support, which comes preconfigured, is for SQLite3. So to create a rails 2.x application with MySql support preconfigured, you simply need to run the following command: rails -d mysql testapp
    This will create the skeleton structure for the rails 2.x application with mysql database adapter.
  3. Configuring the database for the application:-
    Now a database.yml file is created in the /config under your application root folder. You need to set the development, production and test database names there. Also you need to put in the database password,if there exist any,So for your database in this .yml file. This password is same as the one you put in while installing mysql on your machine.

    • So for a rails application, database.yml file configuration is as follows:
      database: testapp_development
      adapter: mysql
      encoding: utf8
      username: root
      password: mypassword
      [Note: password is set here if any]
      socket: /tmp/mysql.sock [NOTE: Similarly for production and test database]
  4. To create the database on the local machine:-
    Run the following command: rake db:create:all
    The above command will create all the three database i.e. development, production and test database as set in the database.yml file
    If you want to start from scratch, you can do rake db:drop:all And in the middle of development we can do rake db:rollback to undo the latest migration file.
  5. To seed the database with initial database:-
    Run the following command: rake db:populate
    This will populated the database with initial data as written in populate rake file. Also a rake task called rebuild has been created so that it becomes easy to do the above steps from dropping a db, re-creating the db,running all the migration and then finally populating it with the initial seed data.
    Run the following command:rake db:rebuild. [NOTE:This rebuild rake task will easily do all the above db related steps. See my previous post for “How to create a rake db:rebuild task”
  6. To run your Rails 2.x application on the local system:-
    Need to first go to root folder of your rails application. For example: F:/Projects/testapp/trunk
    Then run the following command: ruby script/server
    This will run the application in development mode. To run in production mode: ruby script/server -e production
  7. To install rspec plugin for rails application:-
    Run the following commands at the command line:
    ruby script/plugin install
    ruby script/plugin install

    Once the plugin is installed, you must bootstrap your palnglue rails app with RSpec. Stand in the root of your Rails app and run: ruby script/generate rspec
    This will generate the various files needed to use RSpec with Rails.
  8. To run the rspec with rake on the local machine:-
    Run the following command at the root folder of the application: rake spec
    or run specs with scripts/spec command: ruby script/spec spec

B.) How to install a Plugin for Application:–

Stand in the root of your rails app and run:

ruby script/plugin install <here the url path of the plugin to be installed>

C.) Running the RSpec test cases for Models:–

Run using the command:

rake spec:models

D.) Running the RCov:–

Coverage tests can be run using the command:

rake spec:rcov


Posted in Uncategorized | Tagged: , , , , , , , , , , | 3 Comments »

How to use “position:relative” in CSS

Posted by arjunghosh on April 23, 2008

First the definition of CSS position Property:

“The position property places an element in a static, relative, absolute or fixed position.”

“We can also say that the CSS positioning properties allow you to specify the left, right, top, and bottom position of an element. It also allows you to set the shape of an element, place an element behind another, and to specify what should happen when an element’s content is too big to fit in a specified area.”

CSS properties can also be dynamically changed with a JavaScript. The syntax is as follows:"absolute"



Possible Values for the “position” property:

This is Default. An element with position: static always has the position the normal flow of the page gives it (a static element ignores any top, bottom, left, or right declarations).
Normally you wouldn’t specify this unless you needed to override a positioning that had been previously set.

#div-1 {

An element with position: relative moves an element relative to its normal position, so “left:20″ adds 20 pixels to the element’s LEFT position.
Let’s move div-1 down 20 pixels, and to the left 40 pixels:

#div-1 {

An element with position: absolute is positioned at the specified coordinates relative to its containing block. The element’s position is specified with the “left”, “top”, “right”, and “bottom” properties.
Let’s move div-1a to the top right of the page:

#div-1a {

An element with position: fixed is positioned at the specified coordinates relative to the browser window. The element’s position is specified with the “left”, “top”, “right”, and “bottom” properties. The element remains at that position regardless of scrolling. Works in IE7 (strict mode)

An excellent online resource to further understand it is here

Another thing related to CSS positioning is : float
We can “float” an element to push it as far as possible to the right or to the left, and allow text to wrap around it. This is typically used for images, but we can use it for more complex layout tasks.

#div-1a {

Posted in Uncategorized | Tagged: , , , , | 5 Comments »

Rails core has moved to GitHub

Posted by arjunghosh on April 18, 2008

Ya, Ruby on Rails core is now hosted at GitHub, which is uses Git for easy, fast, efficient, distributed version control system and also which looks pretty!!!

In DHH own words,

GitHub has now officially launched and Rails is right there at the premiere. The Rails repository now lives at rails/rails

And you can check it out with:

git clone git://

But you will first need Git installed on your machine. For those on Windows machine, you can get that here: msysgit

Also people should have a look at this excellent post by Tim Pope on how to best use Git with Rails. You can have a look at the full tutorial also.

Posted in Uncategorized | Tagged: , , , , , , | Leave a Comment »

How to freeze Rails application

Posted by arjunghosh on April 18, 2008

Rails is a moving target. Its core is constantly changing. So sometimes it is advisable to “freeze” your rails application so that even if you upgrade and install the latest new version of Rails, it does not break anything in your application.
So first change to your rails application directory:

cd testapp

Then run the following command to freeze this application to the version of Rails that is currently installed on your system:

rake rails:freeze:gems

Now to Unfreezing Rails:

rake rails:unfreeze

Now say you want to freeze to a different version of Rails.No worries. You can freeze to almost any version. This is how:

rake rails:freeze:edge TAG=rel_2-0-2

And to freeze Rails to the current development version:

rake rails:freeze:edge

Freezing helps us when we need to upgrade.For example, we have a Rails application of version 2.0.0 and froze it to that version. Now when Rails version 2.0.2 came out and say you wanted to upgarde, we can upgrade the frozen version of Rails by:

rake rails:freeze:edge TAG=rel_2-0-2

Now suppose after upgrading, you found that your application is not working properly with this new upgrade. So what do you do? Well not to worry. You can easily downgrade back to the previous version:

rake rails:freeze:edge TAG=rel_2-0-0

Posted in Uncategorized | Tagged: , , , , | 1 Comment »

How to determine whether a request was Ajax or not?

Posted by arjunghosh on April 18, 2008

This is something I wanted to remind self also:
Never do the following:

if request.xhr?
render(:update) { |page| ... }

And instead do the following:

respond_to do |format|
format.html { redirect_to(...) }
format.js { render(:update) { |page| ... } }

Posted in Uncategorized | Tagged: , , , | Leave a Comment »

Sanity testing your code

Posted by arjunghosh on April 18, 2008

This is a very simple way of testing your code or hypothesis, but lot of people don’t do it and end up wasting lot of time on exhaustive round of bug testing.
According to Wikipedia:

A sanity test or sanity check is a basic test to quickly evaluate the validity of a claim or calculation. In mathematics, for example, when multiplying by three or nine, verifying that the sum of the digits of the result is a multiple of 3 or 9 (casting out nines) respectively is a sanity test.
In computer science it is a very brief run-through of the functionality of a computer program, system, calculation, or other analysis, to assure that the system or methodology works as expected, often prior to a more exhaustive round of testing.

In software development, the sanity test determines whether it is reasonable to proceed with further testing. Software sanity tests are commonly conflated with smoke tests. A smoke test determines whether it is possible to continue testing, as opposed to whether it is reasonable. A software smoke test determines whether the program launches and whether its interfaces are accessible and responsible (for example, the responsiveness of a web page or an input button). If the smoke test fails, it is impossible to conduct a sanity test. In contrast, the ideal sanity test exercises the smallest subset of application functions needed to determine whether the application logic is generally functional and correct (for example, an interest rate calculation for a financial application). If the sanity test fails, it is not reasonable to attempt more rigorous testing. Both sanity tests and smoke tests are ways to avoid wasting time and effort by quickly determining whether an application is too flawed to merit any rigorous testing.

Posted in Uncategorized | Tagged: , , | Leave a Comment »

What does “ruby script/about” command do?

Posted by arjunghosh on April 17, 2008

Well this is more of a note to self.
You can tell what version of Rails your application uses with this command under your rails application folder:

ruby script/about

Posted in Uncategorized | Tagged: , , , | Leave a Comment »

Steps to Restart a Server on a Linux (ubuntu) Box for Rails, Mongrel and Merb stack

Posted by arjunghosh on April 15, 2008

Actually the title should read as

“Restarting Rails, Merb (a file upload framework) and Mongrel (a lightweight Web Server) Cluster”

Well this is also more of a reminder to self. So here goes the steps as follows:

Step 1: First connect to the Box, where the application is running, using any telnet application like Putty. It can be downloaded from this page. You need to put in the login credentials. For example login: vivanista and its password.

Step 2: Then you need to go to the ‘app’ folder. The command to write at cmd line is:-

cd app

Step 3: Then You need to update the application folder with the latest files from SVN repository.The command to write at cmd line is:-

svn update

Step 4: NOw we need to kill the Mreb process which is running on the server. For that we need to find the Mreb’s process id using the ‘grep’ cmd. The command to write at cmd line is:-

ps ax|grep merb

Step 5: Now you get the process ids i.e. pids of the Mreb you need to kill before restart. You need to kill the pid which has ruby folder as its initial folder in the text. The command to write at cmd line is:-

kill -9 (here the Merb pid to kill)

Step 6: Again run the ‘grep’ cmd of Step 4 to check if the Merb process has been killed properly

Step 7: Now restart Mongrel Cluster.(Note: Advantage of using cluster instead of single instance mongrel server is that it helps in load balancing and hence better performance of the server).The command to write at cmd line is:-

mongrel_rails cluster::restart

Step 8: Then go to the merbuploader folder to restart Merb. So you will be in ‘/app/merbuploader /’. To go ‘merbuploader’ folder, the cmd is:-

cd merbuploader

Step 9: Now to restart Merb, the cmd is:-

merb -d

Step 10: Now to exit the server and putty application, the cmd is:-


Posted in Uncategorized | Tagged: , , , , , , | 1 Comment »

In Rails 2.0, use attribute pairs to create add and remove column migrations.

Posted by arjunghosh on February 14, 2008

We see a very useful feature in Rails 2.0 as defined in Changeset 7422, where now we can generate migration for adding and removing columns by directly passing attribute:type pairs to the migration generator.
Now for example, we need to add a column called ‘dept’ to students table where the model will be called ‘Student’.
Now previously we used something like:-

script/generate migration add_dept_to_student

Now in case of new better way of writing migration in Rails 2.0, we write it as follows:-

script/generate migration AddDeptToStudent dept:string

Here the migration name AddDeptToStudent holds the key. ‘Add’ specifies the we want to add column(s) and ‘Student’ separated by ‘To’ specifies the table.
Similarly, if we need to remove a column ‘dept’ :

script/generate migration RemoveDeptFromStudent dept:string

Similar to above, the migration name RemoveDeptFromStudent holds the key. ‘Remove’ specifies the we want to remove column(s) and ‘Student’ separated by ‘From’ specifies the table.

Posted in Uncategorized | Tagged: , , | 3 Comments »

:disable_with does not work with Merb

Posted by arjunghosh on February 14, 2008

Beside the known problem of :disable_with killing form_remote_tag AJAXiness, I recently faced another one – namely that it seem :disable_with for Submit_tag does not play well with Merb . In the current project I am working, we decided to use Merb for file upload as it is a very fast and lightweight server which was originally built for efficient file uploads. Well, I thought of using the :disable_with option in submit_tag for some fancy submit button state to indicate that the current form is submitting. Well at the first look everything seem fine, and the form got submit. Then after more testing, we found that picture upload in that form via Merb was not working. After some hectic debugging(as :disable_with is the an obvious suspect), we concluded that :disable_with was the culprit. Well the solution to this was a crude hack where we are calling a javascript in the :onsubmit to accessing the submit button and changing the style while the form gets submitted. Well thats what I can think of in short notice. If anyone has a solution for this, please feel free to tell.

Posted in Uncategorized | Tagged: , , , , | Leave a Comment »