rails 101
DESCRIPTION
Rob Cameron, Senor Software Engineer at The Active Network, walks us through the fundamentals of Ruby on Rails.TRANSCRIPT
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.0December 13, 2005
August 2006
Apple announces that Rails will be
installed by default in OSX
Version 2.0December 7, 2007
Version 3.0August 29, 2010
Current Version3.0.10
Conventionover
Configuration
Model
Post
Model
DB Table
Post
posts
Model
DB Table
Controller
Post
posts
PostsController
Model
DB Table
Controller
Index
Post
posts
PostsController
/postsposts_path
Model
DB Table
Controller
Index
Show
Post
posts
PostsController
/postsposts_path
/posts/1post_path(@post)
Model
DB Table
Controller
Index
Show
Edit
Post
posts
PostsController
/postsposts_path
/posts/1post_path(@post)
/posts/1/editedit_posts_path(@post)
Posts table
Primary KeyGenerated and managed by Rails
id
Posts table
Primary KeyGenerated and managed by Rails
Foreign Keybelongs_to
id
user_id
Posts table
Primary KeyGenerated and managed by Rails
Foreign Keybelongs_to
Auto-populatedtimestamps
id
user_id
created_at
updated_at
Posts table
The client-side is important
The client-side is important
PrototypeScriptaculous
The client-side is important
PrototypeScriptaculous
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
Models
ActiveRecord
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
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
GETPOSTPUT
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
GETPUT
index
show
new
create
editupdate
/posts
/posts/1
/posts/new
/posts
/posts/1/edit/posts/1
GET
GET
GET
POST
GETPUTDELETE
index
show
new
create
editupdatedestroy
/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
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 itemend
Deploy to Heroku
git initgit add .git commit -m ‘First commit’heroku create...git push 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 itemend
git initgit add .git commit -m ‘First commit’heroku create...git push heroku master
“Ruby on Rails will never be used in production at Active.”
The End