Tuesday, 15 April 2014

Creating your First Ruby on Rails (RoR) application using scaffolding on Windows with Rails in 7 Steps

We will use Bitnami Ruby stack on Windows Environment through out this article

If you are absolute beginner and need to start from knowing what is Ruby and Rails try below links  
Once you get acquaint with basic syntax of Ruby try out basic ruby code snippets in an interactive shell like web environment here at http://tryruby.org

Prerequisites for this article:
Bitnami Ruby Stack: (You can download the Bitnami installer at URL).

Once installed it starts appearing in your start menu as 
Start > All Programs > Bitnami Ruby Stack

Bitnami Ruby Stack is an Ruby environment with Rails and other software stacks like Apache , MySQL , PostgreSQL. It is good starting point when you start learning RoR development since here you need not struggle with RoR installation and other configurations, Remember that Bitnami Ruby Stack is for development purpose only. And I further feel that Production RoR environment should be using Linux and nginx as Web Container.
So lets start developing our first application in RoR by using scaffolding.

But what is Scaffolding in RoR ?? 
Rails scaffolding is a quick way to generate models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job to be get it done quickly. Scaffolding should be used as a starting point but for creating production scale application many developers prefer writing their own code. Let’s use scaffolding here to generate a simple application in which we will model an application which stores names and their emails.

Let’s begin
Step 1: Open your Ruby console
Go to Start > All Programs > Bitnami Ruby Stack > Use Bitnami Ruby Stack.

Step 2: You will find a console into the directory where you have installed Bitnami Ruby Stack.
In my case its “C:\Bitnami\rubystack-1.9.3-22”

type “ruby –v“ to get verison of Ruby and “rails –v ” to get version of Rails

Through this console you will be creating a new application and will also be generating scaffold application.

Step 3: Type following command to create a new Rails project. Ensure that at this point of time you have active internet connection.
rails new projects/emailcontactsapp
this command creates entire project structure specific to RoR in directory C:\Bitnami\rubystack-1.9.3-22\projects\emailcontactsapp and installs ruby gems required for the application . It stops for a moment , typically 5-10 minutes depending upon your internet bandwidth , till all required gem files are installed.
The output window after successful execution of command will be as

Step 4:  The command executed in Step 3 creates blank project structure with essential files for an RoR application. Once the project has been created its time to create a model and scaffold for the model created. We need to take note that RoR apps are MVC apps so creating model implies that you need to create View and Controller entities. Here since we are using scaffolding approach we only specify the model name and Controllers, Views are generated as to allow basic CRUD style operations.
Lets generate the model with name contacts which would store name and email, hence we will choose our database to contain two fields namely “Name as string” and “Email as string”
cd into the application directory with command
cd  projects\emailcontactsapp
And the command to generate scaffold will be
rails generate scaffold contacts Name:string Email:string
Your command prompt screen should look somewhat like screenshot below after the command successfully executes.

You can observe that we have specified that we are using scaffold and contacts as model name and “Name” of type string and “Email” of type string as two fields of database. 
Here it’s important to know that we are not creating database, it will be created in further steps by issuing command “rake db:migrate” . Also we are using default sqlite3 as database backend for our model. In case you wish to use Database servers like MySQL , Oracle , and others you need to install required gems and specify database connection and access credentials in file
<project-path>/config/database.yml. Let’s stick to sqlite3 as of now.  
Following are further types of fields supported in scaffolding 

Step 5: Now that we have generated Model, Views and Controllers , the only thing remaining is to create database  and appropriate fields in the database which were specified as a part of scaffold command. Inour case those fields were Name and Email.
Its quite easy just execute command
rake db:migrate
Output should be stating that “created migrating …..” look at the screenshot below

Step 6: Now its all done , just we need to start the development server and your contacts RoR app is ready and functioning
To start the development server type the command
rails server
Note down the port number at which development started , in most cases the port number would be 3000. In case if ports are already in use try killing the process which is using the concerned port and retry the command to start development server. 
Alternatively you can also specify the port number at which you intend to start the development server
by command rails server -p <port number>

You can see that in my case development server started at port number 3000
Let’s browse the URL http://localhost:3000/
you will find the default webpage which we are not interested rather we are interested in the Views created by scaffolding.
To view those Views
Go the URL http://localhost:3000/contacts
you will see that I appended the model name which I have created during Step 4.
Below are the screenshots when you visit various links on the application. You can see that you are able to add new contact , delete, modify, and show the contacts.
Step 7: To shutdown the development server hit Cntrl+C

Hope you got your first RoR app running !!! Happy coding !! 

Screenshots of output of application running on development server.


  1. Your post has many detail information. I read all of your article and I genuinely like it. Thank you for discussing this great post.
    - hide online

  2. Basically, the worth and importance of data can be understood by the mere fact that a proper store/warehouse of data is a million times more valuable than a mine of pure gold in the modern world. data science course syllabus

  3. I’m happy I located this blog! From time to time, students want to cognitive the keys of productive literary essays composing. Your first-class knowledge about this good post can become a proper basis for such people. nice one
    data science course in India