Getting Started with Ruby on Rails

Ruby language is growing very fast on the web. This post will introduce you to Ruby on Rails, explain how to install and start a new project with video presentation. We have been working on Ruby on Rails project, demo will available at ruby.9lessons.info. Hope you like this

Ruby is a dynamic, object oriented programming language. It is a scripting language like Python and Perl.

What is Rails?

Ruby on Rails is a web framework written in Ruby. Ruby on Rails makes it easy to built database-backend web application that uses the language Ruby.

Getting Start with Instant Rails:

Instant Rails is a software that provides Rails runtime. This software comes with a bundle contains Ruby, Rails, Apache, and MySQL and all is preconfigured and ready to run.

Downloading & Installing Instant Rails:

1. Go to http://rubyforge.org/projects/instantrails and download latest version of Instant Rails( ZIP version) download link2. Unzip the folder in your favorite location.3. Go to that folder where you extracted Instant Rails and open InstantRails.exe4. When it asks you that the configuration files moved, click OK

5. Click unblock to run it from firewall if it prompts.6. Click on icon and Rails Applications -> Open Ruby Console Window.

8. With this command a bunch of files are created in project_folder folder. To see the files created goto InstantRails->rails_apps->project_folder.9. Now to start rails server locate to project folder what we have created now by using cd project_name (ex: cd demo)

10. Now we are in project folder. From here type ruby script/server.

11. This command will run WEB Server.12. Now open browser and try http://localhost:3000. You should see the welcome screen to make sure that everything is perfect.

The Model-View-Controller Architecture

One of the important feature of Ruby on Rails is it rely on Model-View-Controller architecture (MVC). The main advantage of MVC is separation of Business logic from user interface.

Model: Model will stores the information about each table of a database. It also do some primary validation of data before storing into database.

View: View is generally the interface of your application. Views are often HTML pages with some embedded ruby code. Views handle the job of providing the data to web browser.

Controller: Controller mainly handles the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.

Starting your first project

To start with a simple project follow the simple steps and you should follow each and every line. In this we are creating a welcome project which displays simple text messages.

18. Try to access http://localhost:3000/say/hello/. Now you will see some what dynamic website.

19. See the following diagrams illustrating the url mapping and structure of controllers.

Directory Structure

When you are creating a ruby project with help of helper scripts, it generates the required files and directories required for an application. The directory structure and naming conventions for every project is same and here is the some brief introduction of each directory.

app
It contains all the code of that particular project. It also contains 3 three main folders which reflects the MVC architecture.

app/models
This directory contains the classes which will model and grab the data stored in application’s database.

app/views
The view subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user’s browser.

app/views/layouts
Holds the template files for layouts to be used with views. This models the common
header/footer method of wrapping views. In your views, define a layout using thelayout :default and create a file named default.html.erb. Inside default.html.erb,
call <% yield %> to render the view using this layout.

app/helpers
The helpers directory holds the helper classes and these classes assist the model, view, and controller classes.