Ruby on Rails
is a Model-View-Controller framework for creating database-driven
websites in Ruby. That's a pretty jargon-heavy explanation of what
Rails is, but, believe it or not, it's easier to understand what Rails
is once you understand the pieces. At its core, Rails is built on
simple concepts.
What is a Model-View-Controller Framework?
Model-View-Controller (or MVC) is a way of organizing your code to
hide complexity and contain related code. It will separate a web
application into three primary sections: the model, the controller and
the the view. The model handles all the database interaction, the
controller handles all the web server interaction and the view generates
the HTML code that is actually displayed in the browser.
Keeping everything in its place is a big step toward making complex
web applications easy to implement. If designed correctly, an MVC
application in Rails will never have code in the Controller accessing
the database and the View will never talk to the web server.
The Model's Job in Ruby on Rails
The Model abstracts database "objects" in your web application. So,
for example, if your application is an online store, the "objects" could
be items for sale, the customers or the user reviews. The Model
section of the Ruby code is responsible for storing and retrieving all
objects from the database as well as dealing with any "data integrity"
issues. For instance, if you tried to save an item with a negative
price, the code in the Model should check for that discrepancy and
refuse to save it until the problem is fixed.
The Controller's Job in Ruby on Rails
The Controller is primarily what sits between the Model and the View.
In its simplest form, the Controller fetches objects from the database
using the Model and then hands them to the View to be rendered. It
has direct access to the web server and all cookie
and session variables. A Controller has different "actions" that the
application can perform, such as adding an item to your cart or viewing
an item.
The View's Job in Ruby on Rails
The View takes the data handed to it by the Controller and produces
the HTML output for the web browser. All formatting and styles occur in
the View, leaving the Model and Controller to focus on their specific
jobs. The View is usually a mix of HTML
and Ruby code. This type of code is a lot like PHP (where scripting
code is embedded in HTML), which makes it easy to produce the
formatting and layout tags needed in a web page and to add the dynamic
content from the Model without relying on a third formatting language.
The MVC in Action
The entire process of accessing the example online shop works
something like this. A web browser sends a request to the server for a
URL, such as /items/show/45. This means the web browser wants
to access the Show action of the Items controller. Rails sees this
request, starts the Items controller and runs the Show action.
The Show action then uses the Model to fetch the item with the ID
number of 45. At the same time, the Show action will also gather any
other pertinent variables--such as login information and the cart with
items currently in it--and pass all of these objects from the Model onto
the View. The View uses mixed Ruby and HTML to produce HTML code with
the information from the fetched objects. Rails captures this HTML
output and sends it back to the web browser which displays the generated
page.
MVC with Rails
Ruby on Rails takes all these concepts and bundles them into a single
package for web developers. Every component of the MVC paradigm is
implemented in Ruby and every effort is made to abstract and hide
unnecessary complexity from the progammer. Rails developers only need
to install the rails gem and they'll have a complete development
environment for writing Rails web applications as well as a web server
written in Ruby for running them.