Getting Started with Ruby on Rails
Wall Script
Wall Script
Tuesday, February 08, 2011

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

User Registration using facebook registration plugin

About Author
Ravi Tamada
Ravi Tamada
Designer Developer & Freelance
Chennai, INDIA
androidhive.info

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.
web notification

43 comments:

  1. Great tuts , Waiting for some more good tuts in ruby also. Great job Ravi.

    ReplyDelete
  2. Nice article.....small correction "Movel-View-Controller" as "Model-View-Controller".

    ReplyDelete
  3. nice tutorial about ruby on rails. This has given me a clear understanding on the issue.

    ReplyDelete
  4. Nice tutorial. I've only used CakePHP, so it's nice seeing another MVC framework.

    ReplyDelete
  5. Thanks Dude for this tutorial u rocks

    ReplyDelete
  6. Excellent tutorial i really love the MVC gif

    ReplyDelete
  7. MVC adirindi ra ravi super nuvvu oka MARK zUCKERBERG ayipotavemo future lo. ..........

    ReplyDelete
  8. I have been trying for ages to start Ruby on Rails on Linux Mint 10.10, but have had no luck

    How can you help me?

    ReplyDelete
  9. I am not sure but these may helpful

    http://excid3.com/blog/2010/10/ruby-on-rails-3-and-mysql-on-ubuntu-10-10/

    http://appogee.posterous.com/ubuntu-1010-ruby-on-rails-setup

    http://burakdede.com/2011/01/11/start-with-ruby-on-rails-on-ubuntu-10-10/

    ReplyDelete
  10. OK can any one explain me the difference between using php for accessing database and using ruby on rails for database...i generally keep hearing ruby is better ...can some1 explain why??

    ReplyDelete
  11. Very useful tutorial.Awesome Presentation

    ReplyDelete
  12. Awesome tutorial! I've been looking for a good Ruby on Rails tutorial!

    ReplyDelete
  13. thanks for the tutorial im a beginner so i understand fully and follow all the instructions smoothly

    ReplyDelete
  14. hey can you plz update MySql insert/delete/update with RoR

    ReplyDelete
  15. when i write ruby code within html, the browser prints the ruby code i.e. whatever is written within <%= ... %>. What should b done ?

    ReplyDelete
  16. I've used Rails for years and have seen a lot of tutorials. Yours is definitely one of the best. great job!

    ReplyDelete
  17. easy ROR tutorial for getting started in window, thank

    ReplyDelete
  18. Hi, It is a nice tutorial and a nice way of blogging. Thanks for the post.

    ReplyDelete
  19. Best basic tutorial on the web

    ReplyDelete
  20. Getting:---
    Routing Error No route matches [GET] "/say/hello"

    ReplyDelete
  21. Nice Article.It will help a lot , if you can provide a good tutorial with the same example using MySQL database interaction.

    ReplyDelete
  22. Please sent your tutorial more... thank

    ReplyDelete
  23. I am getting an error like this
    plz help me
    C:\Users\RUBY\Downloads\InstantRails-2.0-win\rails_apps\runapp>ruby script/serve
    r
    => Booting Mongrel (use 'script/server webrick' to force WEBrick)
    => Rails application starting on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    ** Starting Mongrel listening at 0.0.0.0:3000
    Exiting
    C:/Users/RUBY/Downloads/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/mongrel
    -1.1.2-x86-mswin32/lib/mongrel/tcphack.rb:12:in `initialize_without_backlog': On
    ly one usage of each socket address (protocol/network address/port) is normally
    permitted. - bind(2) (Errno::EADDRINUSE)
    from C:/Users/RUBY/Downloads/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8
    /gems/mongrel-1.1.2-x86-mswin32/lib/mongrel/tcphack.rb:12:in `initialize'

    ReplyDelete
  24. Thank you,provide a good tutorial with the same example using MySQL database interaction.
    help me one simple example

    ReplyDelete
  25. The easiest way to setup ruby on rails in the shortest time...Great work keep it up

    ReplyDelete
  26. hi , i m interested to learn , ruby on rails but i dont have programing knowledge accept ruby . can i go for rails????

    ReplyDelete
  27. Add this in routes.rb file
    resources :say do
    collection do
    get 'hello'
    get 'dye'
    end
    end

    ReplyDelete
  28. Nice and easy to learn so if u possible please provide continue videos on ruby on rails

    ReplyDelete

mailxengine Youtueb channel
Make in India
X