Ruby On Rails

What Is Ruby on Rails?

Ruby on Rails is a open source Web Framework designed for database driven internet applications.

Over the last year, it has grown from a side project of 37 Signals and David Heinemeier Hansson to a well-known, popular system used by many small companies and sites such as 43Things, Odeo, Penny Arcade, and more.

But what is it really?

Rails is a Full Stack, MVC (Model-View-Controller) Framework, written in Ruby, consisting of ActiveRecord, ActionController, ActionView, and a few other parts.

It follows a few design patterns and practices:

Why Would I Use Ruby on Rails?

Why Wouldn't I Use Ruby on Rails?

What is Ruby?

Ruby is a scripting language, like PHP or Perl, with a concise, flexible, Object-Oriented syntax with a few more complex features.

You can write Ruby code in a similar style to PHP, but leave off all those pesky semicolons if you want.

def do_something(foo)
  for i = 0; i < 10; i++
    puts foo
  end
end

ActiveRecord

ActiveRecord takes records from a database and makes them into objects you can easily manipulate with very little code.

If you have a table called "people," with the attributes "first_name," "last_name," and "email," all you would need to map it to code would be:

Person < ActiveRecord::Base; end

ActiveRecord

Then you could do this:

Person.create :first_name => "Peter", 
              :last_name => "Cho", 
              :email => "peter@pcho.net"

person = Person.find_by_first_name("Peter")

Person.find(:all).each do |person|
  puts person.first_name
end

Contrast this to PHP

A good bit messier

mysql_query("INSERT INTO people 
(first_name, last_name, email) VALUES
('Peter', 'Cho', 'peter@pcho.net')");

$result = mysql_query("SELECT * FROM people
          WHERE first_name = 'Peter'");
$person = mysql_fetch_assoc($result);

$result = mysql_query("SELECT * FROM people");
while ($row = mysql_fetch_assoc($result)) {
	echo $row['first_name'];
}

ActionController

ActionController organizes your business logic and maps it to URLS.

http://domain.com/people/names/Peter

PeopleController < ApplicationController
  def names
    @people = Person.find_by_first_name(params[:id]) 
  end
end

ActionView

ActionView takes in the data from ActiveController and outputs it as HTML.

<h1>My Favorite People</h1>

<% for person in @people -%>
  <%= person.first_name %><br />
<% end -%>

Bulletin Board Demo

We'll use "scaffolding" to autogenerate a lot of code to quickly build a simple application.

The database tables are already set up, and Rails will automagically be able to use them.

Stop me if something doesn't make sense.

Uml