Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the...

57
Ruby On Rails James Reynolds

Transcript of Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the...

Page 1: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Ruby On RailsJames Reynolds

Page 2: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

What is a Ruby on Rails

Why is it so cool

Major Rails features

Page 3: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Web framework

Code and tools for web development

A webapp skeleton

Developers plug in their unique code

Page 4: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Platforms

Windows

Mac OS X

Linux

Page 5: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Installation

Mac OS X 10.5 will include Rails

Mac OS X 10.4 includes Ruby

Most people reinstall it anyway

From scratch

Drag and drop Locomotive

Page 6: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Databases

Mysql

SQLite

PostgreSQL

DB2

Oracle

Firebird

SQL Server

more

Page 7: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Webservers

Apache w/ FastCGI or Mongrel

LightTPD

WEBrick

Page 8: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

"IDE's"

TextMate and Terminal (preferred)

RadRails

jEdit

Komodo

Arachno Ruby

Page 9: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Has "inspired"Grails

Trails

Sails

Catalyst

Pylons

ColdFusion on Wheels

CakePHP

PHP on TRAX

MonoRail

TrimPath Junction

WASP

And perhaps more...

Page 10: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Why is it so cool?

Using the right tool for the job

y = x^2 vs y = x^0.5

Page 11: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Right tool

Page 12: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 13: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 14: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 15: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 16: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 17: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 18: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 19: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 20: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 21: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 22: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 23: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 24: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a
Page 25: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Rails is the most well thought-out web development framework I've ever used. And that's in a decade of doing web applications for a living. I've built my own frameworks, helped develop the Servlet API, and have created more than a few web servers from scratch. Nobody has done it like this before.

James Duncan Davidson, Creator of Tomcat and Ant

Page 26: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

y = x ^ 2 vs y = x ^ 0.5

Work

Features

Typical Rare

Work

Features

Page 27: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

y = x ^ 2 vs y = x ^ 0.5

Work

Features

Work

Features

Feature ceiling

This is a no-brainer...

Page 28: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Ruby on Rails is a breakthrough in lowering the barriers of entry to programming. Powerful web applications that formerly might have taken weeks or months to develop can be produced in a matter of days.

- Tim O'Reilly, Founder of O'Reilly Media

Page 29: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Major Rails features

Model View Controller

Run Environments

Unit testing

Migrations

Scripts

ActiveSupport

Page 30: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

MVC

Described by Trygve Reenskaug in 1979

Totally ignored in web dev

Except

WebObjects

Struts

JavaServer Faces

Page 31: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

ControllerView

User

Model-View-Controller

sees uses

queries

updates

Model

results

Page 32: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

User

How it works

Deals w/data

Receives URLactions

Produces HTML

Page 33: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

User

Controller.rb

How it works

View.rb

Model.rbsave()

performSave()showSuccess()showError()

HTML file:<form>

<button></form>

clicks submit button(sends url)

Page 34: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

User

How it works

<div>Save was successful

</div>http://server/do?

a=b&c=d

save(:a="b",:c="d")

returns:true

callsshowSuccess() performSave(

:a="b",:c="d" )showSuccess()produces HTML

1

2

34

5

6

8

saves tomysql or flat file orwhatever

7

Page 35: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

HTTPHTML

User

Why use it?

MySQL

EmailEmail

flat fileBDB

Desktop App

Desktop App

Page 36: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

returns:true

callsshowSuccess()

performSave(:a="b",:c="d" )

User

Why use it?

produces HTML

saves toMySQL saves to

flat filesaves to

BDB

mailto:action@server

Button

sends email

updates GUI

http://server/do?a=b&c=d

Page 37: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models

DB backed

Easy to switch db's

Stored on server as files

Stored in session objects

useful for one server

manage with a db for many servers

Page 38: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models

Object relational mapping

tables = classes

rows = arrays of objects

columns = attributes/variables

Maps form submissions to classes

No need to write SQL

Page 39: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models

Naming conventions

Table names are plural

A Class is singular

Table attributes auto discovered

Comes with find method

etc

Page 40: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models - ValidationFields are not empty

Numerically of fields

Uniqueness of text

Matches text to regex

Acceptance (checkbox)

2 password fields same

Length of text

Custom validators

Etc

Page 41: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models - Validationclass Person < ActiveRecord::Base

validates_acceptance_of :terms, :message => "Please accept the terms to proceed"

validates_exclusion_of :age, :in 13..19, :message => "Cannot be a teenager"

validates_format_of :height, :with => /^\d+(in|cm)/

end

Page 42: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models - Connecting

One to one relationshipshas_onebelongs_to

One to many relationshipshas_many

Many to many relationshipshas_and_belongs_to_many

Page 43: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Models - Connecting

class Project < ActiveRecord::Base

belongs_to :portfolio

has_one :manager

has_many :milestones

has_and_belongs_to_many :categories

end

Page 44: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Views

Create html, xml, and js

Flash

flash[:notice] = "Message here"

Ajax built in and dumb simple

Page 45: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Tangent - AJAX

Asynchronous JavaScript and XML

How it works

Mouse click runs a JavaScript

JS contacts server

Server replies with XML

JS parses XML and updates HTML

NO PAGE RELOADS

Page 46: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Tangent - AJAX

Google uses AJAX heavily

Google Maps

Google Calendar

The MS killing Word Processor

Etc

Page 47: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Controllers

Handles external requests to internal methods

Pretty friendly urls

NO: http://example.com/?node=34

Yes: http://example.com/blog/view/34

Caches

Page 48: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Run Environments

Production

Development

Testing

Page 49: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Run Environments

Production

Cached

Freeze Rails

Ship Rails with your app

etc

Page 50: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Run Environments

Development

Reloads source files every time

Scaffold

Page 51: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Run Environments

TestingConnect debugger to running webapp

Stop at breakpointsUnit testing

Integration testingFunctional testingDB is reloaded w/ each testMock and stub code

Page 52: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Unit testing

require 'test/unit'

def mult param1, param2param1 * param2

end

class TestMath < Test::Unit::TestCase def test_simple assert_equal( mult( 4, 4 ), 16 ) endend

Page 53: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Other killer features

Migrations

Add/remove database scheme changes

script/runner

cron for your db

script/console

profiler

Page 54: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Sessions

Cookie based

You just add stuff to the session hash

session[:something] = "value"

Hash stored on the server on disk or in db

Not stored in cookie

Easy to use a db

Page 55: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

ActiveSupport

number_to_currency

5.months + 30.minutes

"cat".pluralize => "cats"

240.megabytes + 2.petabytes

20.minutes.ago

Page 56: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

You can recognize truth by its beauty and simplicity. When you get it right, it is obvious that it is right.

Richard Feynman, Genius Scientist

Page 57: Ruby On Rails - uMac | University of Utah | University of Utah€¦ · 17/01/2007  · Rails is the most well thought-out web development framework I've ever used. And that's in a

Demo