Getting Started with Ruby on Rails


What is Ruby?

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 link

2. Unzip the folder in your favorite location.

3. Go to that folder where you extracted Instant Rails and open InstantRails.exe

4. When it asks you that the configuration files moved, click OK

ruby configuration changed

5. Click unblock to run it from firewall if it prompts.

6. Click on icon and Rails Applications -> Open Ruby Console Window.

Starting ruby console

7. Now Ruby console window is opened. Create a new project using command rails project_name. (ex: rails demo)

Starting ruby console

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)

Starting ruby console

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

Starting ruby console

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.

Starting ruby console
The Model-View-Controller Architecture
Starting ruby console

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.

1. Goto Instant Rails folder and open InstantRails.exe

2. Click on icon and Rails Applications -> Open Ruby Console Window.

Starting ruby console

3. Now Ruby console window is opened create a new project using command rails welcome.

Starting ruby console

4. Now the required files are created by accomplishing the above command.

5. Change to welcome directory by typing cd welcome

Starting ruby console

6. Now we are in welcome directory. To start WEB Server type ruby script/server (Make sure that you are in the welcome directory before starting the server).

Starting ruby console

To confirm server is running or not, open browser and try http://localhost:3000, then you should see the welcome screen.

7. Now leave the Web Server running (do not close the console) open a new console window as described in step2 and locate to welcome directory by typing cd welcome

8. This is the important step as we are going to create a Controller. To create a controller called say type ruby script/generate controller say.

9. Now goto welcome->app->controllers and open say_controller.rb

10. The contents of say_controller.rb will be

say_controller.rb

class SayController < ApplicationController

end

11. Define an actions in say_controller.rb called hello and the code will be

say_controller.rb

class SayController < ApplicationController

def hello

end

end

12. Now goto welcome/app/view/say/ and create a file called hello.html.erb with some html content.

hello.html.erb

<html>

<head>

<tile>

Ruby on Rails

</title>

</head>

<body>

<h1>Welcome to 9lessons</h1>

</body>

</html>

13. Now try to access http://localhost:3000/say/hello/. You will see the output for hello.html.erb

Making the page dynamic

14. So far we are displaying a static page and it is pretty much boring. So just start with adding some dynamic content to our hello.html.erb

15. When you are writing code between <%= and %> that will be considered as ruby code and will be executed.

16. Now open say_controller.rb try following

say_controller.rb

class SayController < ApplicationController

def hello

@title = “Ruby on Rails”

@website = “www.9lessons.info”

end

end

17. To make hello.html.erb dynamic try the code like this.

hello.html.erb

<html>

<head>

<tile>

<%= @title %>

</title>

</head>

<body>

<h1>Welcome to <%= @website %></h1>

Addition : <%= 100 + 300 %>

</body>

</html>

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.

Starting ruby console
Starting ruby console
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/controllers

It contains the controller classes. These controllers mainly handles web requests from users.

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 the

layout :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.

config

Configuration files for the Rails environment (environment.rb), database configuration (database.yml) and routing of incoming user requests (routes.rb).

db

Contains the database schema in schema.rb. You can manage the relational database with scripts you create and place in this directory

doc

This directory is where your application documentation will be stored. This documentation can automatically generated using the command rake doc:app

lib

Contains the application libraries. Generally the code which doesn’t belongs to controllers, models, helpers is stored here and this directory is set in load path.

public

The directory available for the web server. Contains subdirectories for images, stylesheets,

and java scripts.

script

Helper scripts for automation and generation. (example launching a web server)

test

Unit and functional tests along with fixtures. When using the script/generate scripts, template

test files will be generated for you and placed in this directory.

vendor

External libraries such as third party libraries are stored here.

Advertisements