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
About Author
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
5. Click unblock to run it from firewall if it prompts.
6. Click on icon and Rails Applications -> Open Ruby Console Window.
7. Now Ruby console window is opened. Create a new project using command rails project_name. (ex: rails demo)
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.
1. Goto Instant Rails folder and open InstantRails.exe
2. Click on icon and Rails Applications -> Open Ruby Console Window.
3. Now Ruby console window is opened create a new project using command rails welcome.
4. Now the required files are created by accomplishing the above command.
5. Change to welcome directory by typing cd welcome
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).
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
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
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>
<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
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>
<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.
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.
Nice tutorial...!!
ReplyDeleteGreat job Ravi.
ReplyDeleteGreat tuts , Waiting for some more good tuts in ruby also. Great job Ravi.
ReplyDeletegreat job ravi
ReplyDeleteNice article.....small correction "Movel-View-Controller" as "Model-View-Controller".
ReplyDeletenice post keep it up
ReplyDeletenice tutorial about ruby on rails. This has given me a clear understanding on the issue.
ReplyDeleteNice tutorial. I've only used CakePHP, so it's nice seeing another MVC framework.
ReplyDeleteNicely done !
ReplyDeletenice )
ReplyDelete@Anil, @Kevin : Thank U dudes :)
ReplyDeleteThanks Dude for this tutorial u rocks
ReplyDeleteExcellent tutorial i really love the MVC gif
ReplyDeleteMVC adirindi ra ravi super nuvvu oka MARK zUCKERBERG ayipotavemo future lo. ..........
ReplyDelete@karthik : Bava thanx
ReplyDeleteI have been trying for ages to start Ruby on Rails on Linux Mint 10.10, but have had no luck
ReplyDeleteHow can you help me?
I am not sure but these may helpful
ReplyDeletehttp://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/
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??
ReplyDeletenice
ReplyDeletenice tutorial
ReplyDeleteVery useful tutorial.Awesome Presentation
ReplyDeleteAwesome tutorial! I've been looking for a good Ruby on Rails tutorial!
ReplyDeletethanks for the tutorial im a beginner so i understand fully and follow all the instructions smoothly
ReplyDeletehey can you plz update MySql insert/delete/update with RoR
ReplyDeletewhen i write ruby code within html, the browser prints the ruby code i.e. whatever is written within <%= ... %>. What should b done ?
ReplyDelete@swapna
ReplyDeleteWhat is the file extension.
nice tutorial
ReplyDeleteI've used Rails for years and have seen a lot of tutorials. Yours is definitely one of the best. great job!
ReplyDeleteeasy ROR tutorial for getting started in window, thank
ReplyDeleteHi, It is a nice tutorial and a nice way of blogging. Thanks for the post.
ReplyDeleteI'm Loving it
ReplyDeleteBest basic tutorial on the web
ReplyDeletenice one ...
ReplyDeleteGetting:---
ReplyDeleteRouting Error No route matches [GET] "/say/hello"
Nice Article.It will help a lot , if you can provide a good tutorial with the same example using MySQL database interaction.
ReplyDeletePlease sent your tutorial more... thank
ReplyDeleteI am getting an error like this
ReplyDeleteplz 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'
Thank you,provide a good tutorial with the same example using MySQL database interaction.
ReplyDeletehelp me one simple example
The contents were useful :)
ReplyDeleteThe easiest way to setup ruby on rails in the shortest time...Great work keep it up
ReplyDeletehi , i m interested to learn , ruby on rails but i dont have programing knowledge accept ruby . can i go for rails????
ReplyDeleteAdd this in routes.rb file
ReplyDeleteresources :say do
collection do
get 'hello'
get 'dye'
end
end
Nice and easy to learn so if u possible please provide continue videos on ruby on rails
ReplyDelete