75
Web programming with Ruby Lesson 3/4 Saturday, September 11, 2010

Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

Embed Size (px)

Citation preview

Page 1: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 1/75

Web programmingwith Ruby

Lesson 3/4

Saturday, September 11, 2010

Page 2: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 2/75

For this and next lesson

Sinatra

ERB

Rack

Ruby on Rails (a little bit)

Saturday, September 11, 2010

Page 3: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 3/75

Sinatrahttp://www.sinatrarb.com

Saturday, September 11, 2010

Page 4: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 4/75

A domain-specific language builtwith Ruby

Used to develop web applications

Minimalist approach

Focuses of getting web

application up and running fastestway

Saturday, September 11, 2010

Page 5: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 5/75

Installing Sinatra

% sudo gem install sinatra

Saturday, September 11, 2010

Page 6: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 6/75

Hands-on #1

require 'rubygems'require 'sinatra'

get '/' do"Hello world, it's #{Time.now} atthe server!"

end

Saturday, September 11, 2010

Page 7: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 7/75

Run it

Go to http://localhost:4567 onyour browser

% ruby handson1.rb

Saturday, September 11, 2010

Page 8: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 8/75

Page 9: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 9/75

Whenever a HTTP request comes in, it willbe matched in the order it is defined

Example - if POST request sent to http://

localhost/some_url, this route is invoked:

This pattern matching can include namedparameters:

 post ‘/some_url’ do

...end

get ‘/hello/:name’ doputs “Hello #{params[:name]}!”

end

Saturday, September 11, 2010

Page 10: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 10/75

In POST routes, the POST parameters arereceived the same way as the URLparameters

Note that you can get both URL parametersthe same way

 post ‘/hello’ doputs “Hello #{params[:name]}!”

end

 post ‘/hello/:from’ doputs “Hello #{params[:name]} from

#{params[:from]}!”end

Saturday, September 11, 2010

Page 11: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 11/75

Can also filter user-agent, i.e. determine

browser used. Eg Safari browser in iPhone:

To specify a route for iPhone browsers:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_0 likeMac OS X; en-us) AppleWebKit/525.18.1 (KHTML,like Gecko) Version/3.1.1 Mobile/1A543 Safari/

525.20

get ‘/hello’, :agent => /iPhone/ doputs “You are using an iPhone!”

end

Saturday, September 11, 2010

Page 12: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 12/75

Hands-on #2

A simple form processingweb application

Saturday, September 11, 2010

Page 13: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 13/75

Create a simple HTML

form<html><head><link rel="stylesheet" href="http://www.w3.org/

StyleSheets/Core/Traditional" type="text/css"></head><body><h1>A blog post</h1><form method="post"

action="http://localhost:4567/process_form"><label for="title">Title</label><br/><input type=" text" name="title"/><br/>

<label for="content">Content</label><br/><textarea name="content"></textarea><br/><input type="submit" value="submit post"/>

</form></body></html>

Saturday, September 11, 2010

Page 14: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 14/75

Save it as form.html anywhere you want

Saturday, September 11, 2010

Page 15: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 15/75

Page 16: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 16/75

Save it as handson2.rb

Open up form.html on yourbrowser

% ruby handson2.rb

Saturday, September 11, 2010

Page 17: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 17/75Saturday, September 11, 2010

Page 18: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 18/75Saturday, September 11, 2010

Page 19: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 19/75

What about PUT orDELETE?

Saturday, September 11, 2010

Page 20: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 20/75

PUT and DELETE not implemented bybrowsers

Workaround using POST

This invokes:

<form method="post" action="/destroy">

 <input name="_method" value="delete" /> <button type="submit">Destroy</button>

</form>

delete ‘/destroy’ do...

end

Saturday, September 11, 2010

Page 21: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 21/75

Hands-on #3

Create a web application with 4different types of routes

(GET, POST, PUT, DELETE)

Saturday, September 11, 2010

Page 22: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 22/75

Sometimes within a route you want toredirect the user somewhere else

This can be some other route or to anexternal site

This can be done using the redirect helper

The redirect actually sends back a 302

Found HTTP status code to the browser andtells the browser where to o next

Redirection

Saturday, September 11, 2010

Page 23: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 23/75

Redirection examples

redirect '/'redirect 'http://www.google.com'

redirect '/', 303redirect '/somewhere_else', 307

If you don’t want to use the default 302status code, you can redirect with adifferent status code

Saturday, September 11, 2010

Page 24: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 24/75

FiltersSinatra provides a simple filtering mechanism

A before filter runs before every route

An after filter runs after every route

 before do...

end

after do...

end

Saturday, September 11, 2010

Page 25: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 25/75

Static pages

By default, all pages in a folder namedpublic are served out as static pages

For example, if you have a page.html file inthe public folder, you will be able to accessit from http://localhost:4567/page.html 

This means that you can also serve out Javascript libraries, CSS stylesheets andimage files through the same folder.

Saturday, September 11, 2010

Page 26: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 26/75

Static page settings

If you want to change the default folder forstatic pages:

set :public, File.dirname(__FILE__) + '/static'

Saturday, September 11, 2010

Page 27: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 27/75

Hands-on #4

Add some static pages intoyour web application

(HTML, CSS, images)

Saturday, September 11, 2010

Page 28: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 28/75

ViewsViews let you display the information youwant your user to see

In Sinatra, view templates are files used todisplay data returned by the route

By default Sinatra looks for view templates inthe view folder

To change:

set :views, File.dirname(__FILE__) + '/templates'

Saturday, September 11, 2010

Page 29: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 29/75

Views

Some view templating technologies in Ruby:

eRuby (ERB, Erubis etc) (HTML-like)Haml (Ruby-like)

Builder (XML)

Sass (CSS)

Less (CSS)

Saturday, September 11, 2010

Page 30: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 30/75

ERB

HTML-like templating system, one of oldest

Most popular, used widely, was previously

default templating system in Ruby on Raila

<% Ruby code -- inline with output %><%= Ruby expression -- replace with result %><%# comment -- ignored -- useful in testing %>

Saturday, September 11, 2010

Page 31: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 31/75

Hands-on #5

Convert hands-on exercise#2 to using static pages

and views

Saturday, September 11, 2010

Page 32: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 32/75

Copy form.html into thepublic folder

Saturday, September 11, 2010

Page 33: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 33/75

Create a viewCreate this as display.erb in the view folder:

<html><head><link rel="stylesheet" href="http://www.w3.org/

StyleSheets/Core/Traditional" type="text/css">

</head><body><h1><%= @title %></h1><p><%= @content %></p><ul><% @posts.each do |post| %><li><%= post[:title] %></li>

<% end %></ul>

</body><html>

Saturday, September 11, 2010

Page 34: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 34/75

Modify process_form

require 'rubygems'require 'sinatra'

post '/process_form' do

@posts = []3.times do |count|@posts << {:title => "Blog post #{count}"}

end@title = params[:title]@content = params[:content]erb :display

end

Saturday, September 11, 2010

Page 35: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 35/75

Layouts

Layouts are templates used with views,applied to views

By default if you have a file namedlayout.erb (or layout.haml etc) it will beused as the layout for that templating

system

Saturday, September 11, 2010

Page 36: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 36/75

Layout example

Create this as layout.erb and place insideyour views folder:

<html>

<head><link rel="stylesheet" href="http://www.w3.org/StyleSheets/Core/Traditional" type="text/css"></head><body><%= yield %>

</body>

<html>

All your views will use this layout by default,views replacing the yield

Saturday, September 11, 2010

Page 37: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 37/75

Hands-on #6

Add a layout to hands-onexercise #5

Saturday, September 11, 2010

Page 38: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 38/75

Add a layout

Create this as layout.erb and place insideyour views folder:

<html>

<head><link rel="stylesheet" href="http://www.w3.org/StyleSheets/Core/Traditional" type="text/css"></head><body><%= yield %>

</body>

<html>

Saturday, September 11, 2010

Page 39: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 39/75

Page 40: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 40/75

To use another layout, set it when calling theview

To force the view not to use any layouts, setthe layout to false

erb :display, :layout => :another_layout

erb :display, :layout => false

Saturday, September 11, 2010

Page 41: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 41/75

Page 42: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 42/75

Page 43: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 43/75

Error handling

Any other errors will be caught by error

By default error will catchSinatra::ServerError and Sinatra will passyou the error via sinatra.error inrequest.env.

error do'Sorry there was a nasty error - ' +request.env['sinatra.error'].name

end

Saturday, September 11, 2010

Page 44: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 44/75

Error handling

You can also customized error handling likethis:

This catches errors that you raise:

error MyCustomError do'So what happened was...' + request.env['sinatra.error'].message

end

get '/' doraise MyCustomError, 'something bad'

end

Saturday, September 11, 2010

Page 45: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 45/75

Page 46: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 46/75

Saturday, September 11, 2010

Page 47: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 47/75

Ruby on Railshttp://www.rubyonrails.org

Saturday, September 11, 2010

Page 48: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 48/75

Web application

framework written inRuby

Saturday, September 11, 2010

Page 49: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 49/75

Opinionated software, assumes

there is a best way of doingthings

Encourages the ‘best way’ anddiscourages alternative ways

Designed to allow you to writeless code

Saturday, September 11, 2010

Page 50: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 50/75

Don’t Repeat Yourself (DRY)

Convention over ConfigurationREST is best

The Rails Way

Saturday, September 11, 2010

Page 51: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 51/75

Parts of Rails

ActionController -> controller (Sinatra)

ActionView -> view (ERB, Haml etc)

ActiveRecord -> DB model (DataMapper etc)

ActiveResource -> REST model

ActionMailer -> email sending utility

ActiveSupport -> utilities

Saturday, September 11, 2010

Page 52: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 52/75

Routes

Saturday, September 11, 2010

class PostsController < ApplicationController

d f i d

def create@post = Post.new(params[:post])

Page 53: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 53/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }else

format.html { render :action => "new" }format.xml { render :xml => @post.errors, :status=> :unprocessable_entity }

endend

end

def update@post = Post.find(params[:id])

respond_to do |format|if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

Saturday, September 11, 2010

class PostsController < ApplicationController

d f i d

def create@post = Post.new(params[:post])

Page 54: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 54/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }else

format.html { render :action => "new" }format.xml { render :xml => @post.errors, :status=> :unprocessable_entity }

endend

end

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

Saturday, September 11, 2010

class PostsController < ApplicationController

d f i d

def create@post = Post.new(params[:post])

Page 55: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 55/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }else

format.html { render :action => "new" }format.xml { render :xml => @post.errors, :status=> :unprocessable_entity }

endend

end

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

GET

Saturday, September 11, 2010

class PostsController < ApplicationController

def index

def create@post = Post.new(params[:post])

Page 56: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 56/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }elseformat.html { render :action => "new" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

GET

GET

Saturday, September 11, 2010

class PostsController < ApplicationController

def index

def create@post = Post.new(params[:post])

Page 57: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 57/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }elseformat.html { render :action => "new" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

GET

GET

GET

Saturday, September 11, 2010

class PostsController < ApplicationController

def index

def create@post = Post.new(params[:post])

Page 58: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 58/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }elseformat.html { render :action => "new" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

GET

GET

GET

POST

Saturday, September 11, 2010

class PostsController < ApplicationController

def index

def create@post = Post.new(params[:post])

Page 59: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 59/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }elseformat.html { render :action => "new" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

GET

GET

GET

POST

PUT

Saturday, September 11, 2010

class PostsController < ApplicationController

def index

def create@post = Post.new(params[:post])

Page 60: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 60/75

def index@posts = Post.all

respond_to do |format|format.html # index.html.erbformat.xml { render :xml => @posts }

endend

def show@post = Post.find(params[:id])

respond_to do |format|format.html # show.html.erbformat.xml { render :xml => @post }

endend

def new@post = Post.new

respond_to do |format|format.html # new.html.erbformat.xml { render :xml => @post }

endend

def edit

@post = Post.find(params[:id])end

respond_to do |format|if @post.saveflash[:notice] = 'Post was successfully created.'format.html { redirect_to(@post) }format.xml { render :xml => @post, :status

=> :created, :location => @post }elseformat.html { render :action => "new" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def update@post = Post.find(params[:id])

respond_to do |format|

if @post.update_attributes(params[:post])flash[:notice] = 'Post was successfully updated.'format.html { redirect_to(@post) }format.xml { head :ok }

elseformat.html { render :action => "edit" }format.xml { render :xml => @post.errors, :status

=> :unprocessable_entity }end

endend

def destroy@post = Post.find(params[:id])@post.destroy

respond_to do |format|format.html { redirect_to(posts_url) }format.xml { head :ok }

endend

end

GET

GET

GET

GET

POST

PUT

DELETE

Saturday, September 11, 2010

Page 61: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 61/75

Defining routesDefined in config/routes.rb

RESTful routes

Regular routes

map.connect 'post/:id',:controller => 'posts',

:action => 'show',:conditions => { :method => :get }

map.resource :posts

Saturday, September 11, 2010

Page 62: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 62/75

Saturday, September 11, 2010

Page 63: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 63/75

Provides a minimal API to

connect web servers andweb frameworks

Saturday, September 11, 2010

Page 64: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 64/75

A Rack application must:

Take a hash input of CGI-like environment(HTTP request)

Respond to a #call method that returnsan array of:

status code

environment (hash)

response body

Saturday, September 11, 2010

Page 65: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 65/75

Middleware

Saturday, September 11, 2010

Page 66: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 66/75

HTTP

App

Middleware

Saturday, September 11, 2010

Page 67: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 67/75

request

AppFirst

response

Second

Saturday, September 11, 2010

Page 68: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 68/75

Page 69: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 69/75

first.rb

class Firstdef initialize(app)@app = app

end

def call(env)puts "CALLED FIRST"status, headers, response = @app.call(env)response << "<h1>Adding FIRST module</h1>"headers["Content-Length"] =

response.to_s.length.to_s[status, headers, response]

endend

Saturday, September 11, 2010

Page 70: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 70/75

second.rb

class Seconddef initialize(app)@app = app

end

def call(env)puts "CALLED SECOND"status, headers, response = @app.call(env)response << "<h1>Adding SECOND module</h1>"headers["Content-Length"] =

response.to_s.length.to_s[status, headers, response]

endend

Saturday, September 11, 2010

Page 71: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 71/75

Saturday, September 11, 2010

Page 72: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 72/75

Saturday, September 11, 2010

Page 73: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 73/75

How is this useful?

Modular - split the workload

Clean way to write different kinds of web

applications that does specialized work andchain them together

Saturday, September 11, 2010

Page 74: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 74/75

request

RailsSinatra

response

Merb

Saturday, September 11, 2010

Page 75: Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

8/8/2019 Ruby Course - Lesson 3 and 4 - Web Programming With Ruby

http://slidepdf.com/reader/full/ruby-course-lesson-3-and-4-web-programming-with-ruby 75/75

Questions?