Getting Started with Ruby on Rails
by Erik Andrejko
March 20, 2009
|
Ruby on Rails is a popular framework for creating web applications and is especially suited for the rapid development of rich internet applications.
|
Ruby on Rails is a popular framework for creating web
applications and is especially suited for the rapid
development of rich internet applications. Built on top of
the object oriented language Ruby, Ruby on Rails includes a
collection of tools and a template system for quickly
building web applications. In many ways, Ruby on Rails is
also a philosophy and you will find this influencing the way
that web applications are built in Rails. As a framework,
Ruby on Rails is organized in the Model-View-Controller
(MVC) architecture. Integrated with Ruby on Rails are the
popular Prototype and Scriptaculous JavaScript libraries
that can be used to enhance a web application with AJAX.
Installation
Installation of Ruby on Rails requires installing Ruby
and Ruby on Rails. Ruby may be installed on a variety of
platforms and can be downloaded from: http://www.ruby-
lang.org/en/downloads/. For installation on Windows,
the best option is to install the “one-click
installer”.
Ruby organizes its packages into ‘gems’ and
the gem command is used to access the Ruby
package management system. Once Ruby has been installed it
is a good idea to update gem using the
command:
gem update --system
Now that Ruby has been installed, the next step is to
install Rails. Rails is distributed as a Ruby gem and can
be installed just like any other gem. To install the Rails
gem use the command:
gem install rails
The Rails gem will be installed with all of the necessary
dependencies. On a Unix based system, like Linux or Mac OS
X, it is often necessary to prefix all gem commands with the
sudo command, as installing gems requires
administrator rights. You will frequently see this pattern
or prefixing the gem command with
sudo mentioned online. In this case, the rails
gem would be installed with:
sudo gem install rails
Creating a Ruby on Rails Application
Part of the philosophy of Rails includes using commands
to generate code. The rails command is used to
create a new Ruby on Rails project. To create the
hello_world project use the command:
rails hello_world
This will create a Rails application directory structure
in the hello_world subdirectory. Since Rails
has a philosophy of convention over configuration every
Rails application has the same directory structure and files
in the same locations. The generated directory structure
contains the following:
hello_world
|____app
| |____controllers
| |____helpers
| |____models
| |____views
|____config
|____db
|____doc
|____lib
|____log
|____public
| |____images
| |____javascripts
| |____stylesheets
|____script
|____test
|____tmp
|____vendor
As you can see, a new Rails project starts with a lot of
structure. Some of the most important directories are:
- app: stores the models, controllers and views of the application
- public: this directory is used to store static HTML files, images, JavaScript and CSS files
- log: stores log files of all requests and errors
- script: contains utility functions to generate code to use in your application
Since Rails emphasizes convention over configuration,
once you learn in which directories to find the various
parts of an application, it will be the same for every Rails
application that you encounter.
Getting Started with Ruby on Rails
Starting the Local Server
|