Rails 101
Rails is an MVC framework for Ruby focused on building web applications
David Heinemeier Hansson (DHH)
First open sourced July 2004
Extracted from Basecamp
Version 1.0 December 13, 2005
 
August 2006 Apple announces that Rails will be installed by default in OSX Apple announces that Rails will be installed by default in OSX Apple announces that Rails will be installed by default in OSX
Version 2.0 December 7, 2007 Version 3.0 August 29, 2010 August 29, 2010 August 29, 2010 August 29, 2010 August 29, 2010
Current Version 3.0.10 3.0.10
Convention over Configuration Configuration Configuration
Model Post
Model DB Table Post posts
Model DB Table Controller Post posts PostsController
Model DB Table Controller Index Post posts PostsController /posts posts_path
Model DB Table Controller Index Show Post posts PostsController /posts posts_path /posts/1 post_path(@post)
Model DB Table Controller Index Show Edit Post posts PostsController /posts posts_path /posts/1 post_path(@post) /posts/1/edit edit_posts_path(@post)
Posts table
Primary Key Generated and managed by Rails id Posts table
Primary Key Generated and managed by Rails Foreign Key belongs_to id user_id Posts table
Primary Key Generated and managed by Rails Foreign Key belongs_to Auto-populated timestamps id user_id created_at updated_at Posts table
The client-side is important
The client-side is important Prototype Scriptaculous
The client-side is important Prototype Scriptaculous jQuery jQuery jQuery jQuery jQuery jQuery
Generators
Create a new app
Structure
 
Majority of code
Majority of code App/environment config
Majority of code App/environment config Database config (and database!)
Majority of code App/environment config Database config (and database!) App documentation
Majority of code App/environment config Database config (and database!) App documentation Custom code
Majority of code App/environment config Database config (and database!) App documentation Custom code Images, javascript, stylesheets
Majority of code App/environment config Database config (and database!) App documentation Custom code Images, javascript, stylesheets Rails scripts
Majority of code App/environment config Database config (and database!) App documentation Custom code Images, javascript, stylesheets Rails scripts Test code
Majority of code App/environment config Database config (and database!) App documentation Custom code Images, javascript, stylesheets Rails scripts Test code Third party code
WEBrick
Scaffolding
Scaffolding Creates CRUD screens for working with a single database table Creates CRUD screens for working with a single database table Creates CRUD screens for working with a single database table Creates CRUD screens for working with a single database table
Models
ActiveRecord Object Relational Mapping Object Relational Mapping Object Relational Mapping
Database
Migrations
Relationships
Rails console
Validations
Controllers
Actions
Routes
params
session
@
format
“ The essence of XML is this: the problem it solves is not hard, and it does not solve the problem well.”  — Phil Wadler —  Phil Wadler —  Phil Wadler
Views
Layouts
Partials
Form helpers
Helpers
html_safe
RESTful
Free web services
ws-*
“ Don’t blame me, I’m just a contractor.”
Let HTTP do what it was meant to do
GET POST PUT DELETE DELETE DELETE DELETE
resources
GET index /posts
GET GET index show /posts /posts/1
GET GET GET index show new /posts /posts/1 /posts/new
GET GET GET POST index show new create /posts /posts/1 /posts/new /posts
GET GET GET POST GET index show new create edit /posts /posts/1 /posts/new /posts /posts/1/edit
GET GET GET POST GET PUT index show new create edit update /posts /posts/1 /posts/new /posts /posts/1/edit /posts/1
GET GET GET POST GET PUT DELETE index show new create edit update destroy /posts /posts/1 /posts/new /posts /posts/1/edit /posts/1 /posts/1
Caching
Fragment caching
Action caching
Page caching
“ There are only two hard problems in Computer Science: cache invalidation and naming things.” — Phil Karlton —  Phil Karlton —  Phil Karlton
Testing
Unit
Functional
Integration
Configuration
Environments
Deployment
 
Questions?
Workshop
Web interface to the Active.com Search API
Example
What you need to know
Required Gems
Required Gems # Gemfile gem 'httparty' gem 'heroku'
Required Gems # Gemfile gem 'httparty' gem 'heroku' rob$ bundle install
Required Gems # Gemfile gem 'httparty' gem 'heroku' rob$ bundle install rob$ gem install bundler
Search API Endpoint http://api.amp.active.com/search?       v=json&          k=keywords&          m=meta:channel%3DRunning&          api_key=wuhmn9ye94xn3xnteudxsavw
HTTP call HTTParty.get(url)
Parse JSON JSON.parse(text) HTTP call HTTParty.get(url)
Iterate through array my_array.each do |item| puts item end
Deploy to Heroku git init g it add . g i t commit -m ‘First commit’ he r oku create ... git  p ush heroku master
Questions?
# Gemfile gem 'httparty' gem 'heroku' rob$ bundle install rob$ gem install bundler http://api.amp.active.com/search?       v=json&          k=keywords&          m=meta:channel%3DRunning&          api_key=wuhmn9ye94xn3xnteudxsavw JSON.parse(text) HTTParty.get(url) my_array.each do |item| puts item end git init g it add . g i t commit -m ‘First commit’ he r oku create ... git  p ush heroku master
“ Ruby on Rails will never be used in production at Active.”
The End

Rails 101