• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Rails 3 from A to Z
 

Rails 3 from A to Z

on

  • 2,373 views

Presentation on fundamentals of Rails and Rails 3 from CodeMash, January 2011.

Presentation on fundamentals of Rails and Rails 3 from CodeMash, January 2011.

Statistics

Views

Total Views
2,373
Views on SlideShare
1,555
Embed Views
818

Actions

Likes
1
Downloads
0
Comments
0

3 Embeds 818

http://tumblr.mattyoho.com 812
http://digimatt.tumblr.com 5
http://www.tumblr.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • http://www.flickr.com/photos/tubemail/3002309450/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://engineering.attinteractive.com/2010/10/arel-two-point-ohhhhh-yaaaaaa/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/
  • http://www.flickr.com/photos/26666081@N03/5154902507/
  • http://www.flickr.com/photos/dlanod/235990854/
  • http://www.flickr.com/photos/curiousexpeditions/2355395524/
  • http://www.flickr.com/photos/26666081@N03/5154866369/
  • http://www.flickr.com/photos/tubemail/3001472687/
  • http://www.flickr.com/photos/tubemail/3001472687/
  • http://www.flickr.com/photos/tubemail/3001472687/
  • http://www.flickr.com/photos/tubemail/3001472687/
  • http://www.flickr.com/photos/tubemail/3001472687/
  • http://www.flickr.com/photos/curiousexpeditions/2354556261/
  • http://www.flickr.com/photos/sohvimus/3228836603/
  • http://www.flickr.com/photos/sohvimus/3228836603/
  • http://www.flickr.com/photos/danarah/3637584542/
  • http://www.flickr.com/photos/sohvimus/3228836603/
  • http://www.flickr.com/photos/sohvimus/3228836603/

Rails 3 from A to Z Rails 3 from A to Z Presentation Transcript

  •  
  •  
  • @speaker = { :name => ‘Matt Yoho’ , :company => EdgeCase , :writes => [ ‘Ruby on Rails’ ], :loves => [ ‘Comic Books’ ] }
  • Goals
  • Rails is a Domain Specific Language for web applications written in and on top of Ruby.
  • Rails 1.0 December 2005
  • Rails 2.0 December 2007
  • Rails 2.3 March 2009
  • Rails 3 .0 August 2010
  • Rails 3 .0.3 November 2010
  • Agenda
  • 1) Rails Overview
  • 2) Highlight new features
  • 3) Example app
  • Rails concepts
  • MVC
  • M odel VC
  • MV iew C
  • MV C ontroller
  • Models Views Controllers Model View Controller
  • Models Views Controllers Helpers Model View Controller
  • Models Views Controllers Routes Helpers Model View Controller
  • class Movie < ActiveRecord :: Base has_many :nights belongs_to :genre validates :title , :presence => true validates :imdb_id , :presence => true , :uniqueness => true before_save :parse_posters def poster_url(size = :thumb ) self .posters.find do | p | p =~ / #{ size } .(jpg|png)Z/i end end end Model Domain logic, CRUD operations. Inherit from ActiveRecord :: Base
  • Model Domain logic, data persistence. Implement ActiveModel in Rails 3 class Movie < ActiveRecord :: Base has_many :nights belongs_to :genre validates :title , :presence => true validates :imdb_id , :presence => true , :uniqueness => true before_save :parse_posters def poster_url(size = :thumb ) self .posters.find do | p | p =~ / #{ size } .(jpg|png)Z/i end end end
  • Model Associations Validations Lifecycle callbacks class Movie < ActiveRecord :: Base has_many :nights belongs_to :genre validates :title , :presence => true validates :imdb_id , :presence => true , :uniqueness => true before_save :parse_posters def poster_url(size = :thumb ) self .posters.find do | p | p =~ / #{ size } .(jpg|png)Z/i end end end
  • < div class= ”movie” > < img src= ” <%= @movie .poster_url %> ” alt= ” <%= @movie .title %> ” /> < h3 ><%= @movie .title %></ h3 > <%= link_to( “See this movie” , movies_path( @movie )) %> </ div > View Presentation templates. Inherit from ActionView :: Base Erb and HAML
  • View Nested structure. Layouts Templates Partials Partials
  • View Contain < head > and < body > tags, basic stylesheets and JavaScript links. One or a few per app. Layouts
  • View Templates Contain page-specific markup and exist for approximately each controller action.
  • View Small, reusable template components. Partials Partials
  • class MoviesController < ApplicationController before_filter :user_of_age? def show id = params[ :id ] @movie = Movie .find(id) render ‘movies/show’ end private def user_of_age? unless current_user.age >= 17 redirect_to(root_path) end end end Controller Orchestrators that coordinate data retrieval and persistence, handle request and response bookkeeping, and render views to client. Inherit from ActionController :: Base (In practice, ApplicationController )
  • Controller Rendering Redirection Filters class MoviesController < ApplicationController before_filter :user_of_age? def show id = params[ :id ] @movie = Movie .find(id) render ‘movies/show’ end private def user_of_age? unless current_user.age >= 17 redirect_to(root_path) end end end
  • module MoviesHelper def movie_name(night) if night.movie.present? night.movie_title else &quot;DUNNO YET&quot; end end end end Helpers Presentation logic that should be kept out of templates but does not belong on models. Modules, not classes. Presenter models are an alternative.
  • Movies :: Application .routes.draw do resources :movies do resources :votes , :only => [ :create , :destroy ] end end end Routes Mappings from incoming requests to controller actions (or occasionally elsewhere.) Rails 3 brings new capabilities.
  • class Notifier < ActionMailer :: Base default :from => &quot;invites@movienight.com&quot; , :subject => &quot;You are invited!&quot; def invited (invitation) headers[ :content_type ] = 'text/html' @night = invitation.night @invitee = invitation.invitee mail :to => invitation.email endend end end Mailer Controller-like classes that render mail messages. Can handle multi-part (plain text/HTML) and attachments. Inherit from ActionMailer::Base
  • DRY
  • Don’t Repeat Yourself
  • class CreateMovies < ActiveRecord :: Migration def self .up create_table :movies do | t | t.string :title t.string :imdb_id t.datetime :release t.text :posters t.timestamps end end def self .down drop_table :movies end end Migrations
  • class Movie < ActiveRecord :: Base validates :title , :presence => true validates :imdb_id , :presence => true , :uniqueness => true before_save :parse_posters def poster_url( size = :thumb ) self .posters.find do |p| p =~ / #{size} .(jpg|png)Z/i end end end Model
  • ConventionConfiguration
  • REST
  • Define APIs
  • Define RESTful APIs
  • Resources
  • Identified by URIs
  • http://movienight.com/movies/123
  • Movie id = 123 ... http://movienight.com/movies/123
  • May be nested
  • http://movienight.com/movies/123/actors/4
  • Movie id = 123 http://movienight.com/movies/123/actors/4 Actor id = 4
  • May have multiple representations
  • http://movienight.com/movies/123.json
  • http://movienight.com/movies/123.json {&quot;movie&quot; : {&quot;alt_title&quot; : &quot;Batman Begins&quot;, &quot;created_at&quot; : &quot;2011-01-07T15:51:50Z&quot;, &quot;id&quot; :33, &quot;imdb_id&quot; : &quot;tt0096251&quot;, &quot;popularity&quot; : 2.0, &quot;posters&quot; : [&quot;http://images.themoviedb.org/posters/1113/poster2rz_thumb.jpg &quot;], &quot;release&quot; : &quot;2005-06-17T00:00:00Z&quot;, &quot;title&quot; : &quot;Batman Begins&quot;, &quot;tmdb_id&quot; : &quot;18393&quot;, &quot;updated_at&quot; : &quot;2011-01-07T15:51:50Z&quot;}} {JSON}
  • http://movienight.com/movies/123.xml
  • http://movienight.com/movies/123.xml <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <movie> <title>Batman Begins</title> <created-at type=&quot;datetime&quot;> 2011-01-06T22:18:44Z </created-at> <id type=&quot;integer&quot;>13</id> <imdb-id>tt0372784</imdb-id> <release type=&quot;datetime&quot;> 2005-06-17T00:00:00Z </release> <tmdb-id>272</tmdb-id> </movie> <xml>
  • Rely on HTTP content type negotiation
  • HTTP headers
  • HTTP headers http://movienight.com/movies/123.json
  • Accept: application/json, text/javascript, */* ; Request header
  • Content-Type: application/json; Response header
  • CRUD via HTTP VERBS
  • GET POST PUT DELETE HTTP verbs
  • READ CREATE UPDATE DELETE CRUD operations
  • SHOW CREATE UPDATE DESTROY Rails controller actions
  • SHOW CREATE UPDATE DESTROY NEW EDIT INDEX
  • GET /movies/123 HTTP/1.1 Accept:text/html
  • GET /movies/123 HTTP/1.1 Accept:text/html CRUD op
  • GET /movies/123 HTTP/1.1 Accept:text/html Resource
  • GET /movies/123 HTTP/1.1 Accept:text/html MoviesController#show
  • Strongly encouraged by Rails
  • Request -> Response Cycle
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client
  • Models Views Controllers Routes Request-response cycle Client Webpage!
  • Feature details
  •  
  •  
  • Bundler
  • Commands
  • rails new ~/app/name
  • rails new ~/app/name -T -J
  • rails new ~/app/name -T -J -T = Skip Test::Unit
  • rails new ~/app/name -T -J -J = Skip Prototype
  • rails generate < thing >
  • rails server
  • rails console
  • Dependency Management
  • can't activate cucumber (= 0.4.4, runtime) for [], already activated cucumber-0.6.1 for [] (Gem::LoadError)
  • $ gem install rails -v2.3.10Successfully installed activesupport-2.3.9Successfully installed activerecord-2.3.9Successfully installed actionpack-2.3.9Successfully installed actionmailer-2.3.9Successfully installed activeresource-2.3.9Successfully installed rails-2.3.96 gems installed 2.3
  • $ gem install rails -v3.0.3Successfully installed activesupport-3.0.3Successfully installed builder-2.1.2Successfully installed i18n-0.5.0Successfully installed activemodel-3.0.3Successfully installed rack-1.2.1Successfully installed rack-test-0.5.7Successfully installed rack-mount-0.6.13Successfully installed tzinfo-0.3.23Successfully installed abstract-1.0.0Successfully installed erubis-2.6.6Successfully installed actionpack-3.0.3Successfully installed arel-2.0.6Successfully installed activerecord-3.0.3Successfully installed activeresource-3.0.3Successfully installed mime-types-1.16Successfully installed polyglot-0.3.1Successfully installed treetop-1.4.9Successfully installed mail-2.2.13Successfully installed actionmailer-3.0.3Successfully installed thor-0.14.6Successfully installed railties-3.0.3Successfully installed bundler-1.0.7Successfully installed rails-3.0.323 gems installed 3.0
  • $ gem install rails -v3.0.3Successfully installed activesupport-3.0.3Successfully installed builder-2.1.2Successfully installed i18n-0.5.0Successfully installed activemodel-3.0.3Successfully installed rack-1.2.1Successfully installed rack-test-0.5.7Successfully installed rack-mount-0.6.13Successfully installed tzinfo-0.3.23Successfully installed abstract-1.0.0Successfully installed erubis-2.6.6Successfully installed actionpack-3.0.3Successfully installed arel-2.0.6Successfully installed activerecord-3.0.3Successfully installed activeresource-3.0.3Successfully installed mime-types-1.16Successfully installed polyglot-0.3.1Successfully installed treetop-1.4.9Successfully installed mail-2.2.13Successfully installed actionmailer-3.0.3Successfully installed thor-0.14.6Successfully installed railties-3.0.3Successfully installed bundler-1.0.7Successfully installed rails-3.0.323 gems installed 3.0
  • Gemfile
  • source 'http://rubygems.org' gem 'rails' , '~> 3.0.3' gem 'basic_assumption' , '~> 0.4.1' gem 'devise' , '~> 1.1.5' gem 'dynamic_form' , '~> 1.1.3' gem 'haml' , '~> 3.0.24' gem 'nokogiri' , '~> 1.4.4' gem 'pg' , '~> 0.9.0' gem 'ruby-debug' , '~> 0.10.3' , :platforms => :mri_18 gem 'ruby-debug19' , '~> 0.11.6' , :platforms => :mri_19 group :test do gem 'rspec-rails' , '~> 2.4.1' gem 'factory_girl_rails' , '~> 1.0.0' gem 'fakeweb' , '~> 1.3.0' end end end
  • source 'http://rubygems.org'
  • gem 'rails' , '~> 3.0.3'
  • gem 'rails' , '~> 3.0.3' Name
  • gem 'rails' , '~> 3.0.3' Version
  • :mri :mri_18 :mri_19 :mswin :jruby Platforms
  • gem 'ruby-debug' , :platforms => :mri_18
  • gem 'ruby-debug19' , :platforms => :mri_19
  • Install from Git
  • gem 'basic_assumption' , :git => 'git://github.com/mattyoho/basic_assumption.git' , :branch => 'experimental'
  • gem 'basic_assumption' , :git => 'git://github.com/mattyoho/basic_assumption.git' , :branch => 'experimental' GitHub repo
  • gem 'basic_assumption' , :git => 'git://github.com/mattyoho/basic_assumption.git' , :branch => 'experimental' Git repo branch
  • bundle install
  • group :test do gem 'rspec-rails' gem 'cucumber' gem 'fakeweb' end end
  • bundle install --without test
  • bundle open < gem name >
  • bundle open < gem name >
  • bundle update
  • bundle package
  • http://gembundler.org/ http://gembundler.com/man/gemfile.5.html
  • MATT YOHO RAILS 3 FROM A TO Z Models MATT YOHO RAILS 3 FROM A TO Z
  • The great split
  • ActiveRecord MATT YOHO RAILS 3 FROM A TO Z
  • ActiveRecord ActiveModel ActiveRelation
  • ActiveModel
  • ActiveModel :: Validations ActiveModel :: Callbacks ActiveModel :: Errors ActiveModel :: Dirty
  • validates :title , :presence => true :format => /[a-zA-Z]/
  • ActiveModel :: Lint
  • Mongoid Datamapper
  • ActiveRelation
  • Not as speedy?
  • @tenderlove Comeplete rewrite of Arel
  • Movie . select( ‘*’ ) where( “title ILIKE %batman%” ). order( ‘release DESC’ )
  • criteria = Movie . select( ‘*’ ) where( “title ILIKE %batman%” ). order( ‘release DESC’ )
  • criteria = Movie . select( ‘*’ ) where( “title ILIKE %batman%” ). order( ‘release DESC’ ) criteria.to_sql
  • &quot;SELECT * FROM &quot;movies&quot; WHERE (title ILIKE '%batman%') ORDER BY title&quot;
  • select() where() order() joins() includes() limit() group() having() etc.
  • named_scope
  • scope
  • scope :batman_flicks , where( “title ILIKE %batman%” )
  • default_scope order( “title” )
  • MATT YOHO RAILS 3 FROM A TO Z Controllers
  • MATT YOHO RAILS 3 FROM A TO Z ActionController respond_to
  • MATT YOHO RAILS 3 FROM A TO Z ActionController class MoviesController < ApplicationController def show @movie = Movie .find(params[:id]) respond_to do | format | format.json { render :json => @movie .to_json } format.xml { render :json => @movie .to_xml } format.html { render ‘movies/show’ } end endend end
  • MATT YOHO RAILS 3 FROM A TO Z ActionController respond_with
  • MATT YOHO RAILS 3 FROM A TO Z ActionController class MoviesController < ApplicationController respond_to :json , :xml , :html def show @movie = Movie .find(params[:id]) respond_with( @movie , :status => :ok ) endend end
  • MATT YOHO RAILS 3 FROM A TO Z ActionMailer class Mailer < ActionMailer :: Base default :from => &quot;invites@movienightapp.com&quot; , :subject => &quot;You’ve been invited to a movie night!&quot; def invitation( invitation ) headers[ :content_type ] = 'text/html' @night , @invitee = invitation.night, invitation.invitee mail :to => invitation.email endend end
  • MATT YOHO RAILS 3 FROM A TO Z ActionMailer class Mailer < ActionMailer :: Base default :from => &quot;invites@movienightapp.com&quot; , :subject => &quot;You’ve been invited to a movie night!&quot; def invitation( invitation ) headers[ :content_type ] = 'text/html' @night , @invitee = invitation.night, invitation.invitee mail :to => invitation.email endend end message = Mailer .invitation( @invitation ) message.deliver
  • MATT YOHO RAILS 3 FROM A TO Z mail gem
  • MATT YOHO RAILS 3 FROM A TO Z ActionMailer :: Base AbstractController::Base ActionController :: Base
  • Views
  • Whitelist, not blacklist
  • <%= h ( @movie .description) %>
  • <%= @movie .description %>
  • <%= raw( “<h2> #{ @movie .title } </h2>” ) %>
  • <%= raw( “<h2> #{ @movie .title } </h2>” ) %> String #html_safe
  • Unobtrusive JS
  • link_to_remote &quot;Destroy&quot; , :url => movie_url( @movie ), :method => :delete
  • <a href=&quot;#&quot; onclick=&quot;new Ajax.Request('/movies/4', {asynchronous:true, evalScripts:true, method:'delete'}); return false;&quot;>Destroy</a> link_to_remote &quot;Destroy&quot; , :url => movie_url( @movie ), :method => :delete
  • link_to &quot;Destroy&quot; , movie_url( @movie ), :remote => true , :method => :delete
  • <a href=&quot;/movies/4&quot; data-remote=”true&quot; data-method=”delete”>Destroy</a> link_to &quot;Destroy&quot; , movie_url( @movie ), :remote => true , :method => :delete
  • https://github.com/rails/jquery-ujs
  • Routing
  • Pneumatic tubes, popular in the late 19th and early 20th century for transporting messages, cash, etc.
  • Text Resources
  • CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans end end
  • cup_shots GET /cups/:cup_id/shots(.:format) {:action=>&quot;index&quot;, :controller=>&quot;shots&quot;} POST /cups/:cup_id/shots(.:format) {:action=>&quot;create&quot;, :controller=>&quot;shots&quot;} new_cup_shot GET /cups/:cup_id/shots/new(.:format) {:action=>&quot;new&quot;, :controller=>&quot;shots&quot;} edit_cup_shot GET /cups/:cup_id/shots/:id/edit(.:format) {:action=>&quot;edit&quot;, :controller=>&quot;shots&quot;} cup_shot GET /cups/:cup_id/shots/:id(.:format) {:action=>&quot;show&quot;, :controller=>&quot;shots&quot;} PUT /cups/:cup_id/shots/:id(.:format) {:action=>&quot;update&quot;, :controller=>&quot;shots&quot;} DELETE /cups/:cup_id/shots/:id(.:format) {:action=>&quot;destroy&quot;, :controller=>&quot;shots&quot;} cups GET /cups(.:format) {:action=>&quot;index&quot;, :controller=>&quot;cups&quot;} POST /cups(.:format) {:action=>&quot;create&quot;, :controller=>&quot;cups&quot;} new_cup GET /cups/new(.:format) {:action=>&quot;new&quot;, :controller=>&quot;cups&quot;} edit_cup GET /cups/:id/edit(.:format) {:action=>&quot;edit&quot;, :controller=>&quot;cups&quot;} cup GET /cups/:id(.:format) {:action=>&quot;show&quot;, :controller=>&quot;cups&quot;} PUT /cups/:id(.:format) {:action=>&quot;update&quot;, :controller=>&quot;cups&quot;} DELETE /cups/:id(.:format) {:action=>&quot;destroy&quot;, :controller=>&quot;cups&quot;} coffee_beans GET /coffee_beans(.:format) {:action=>&quot;index&quot;, :controller=>&quot;coffee_beans&quot;} POST /coffee_beans(.:format) {:action=>&quot;create&quot;, :controller=>&quot;coffee_beans&quot;} new_coffee_bean GET /coffee_beans/new(.:format) {:action=>&quot;new&quot;, :controller=>&quot;coffee_beans&quot;}edit_coffee_bean GET /coffee_beans/:id/edit(.:format) {:action=>&quot;edit&quot;, :controller=>&quot;coffee_beans&quot;} coffee_bean GET /coffee_beans/:id(.:format) {:action=>&quot;show&quot;, :controller=>&quot;coffee_beans&quot;} PUT /coffee_beans/:id(.:format) {:action=>&quot;update&quot;, :controller=>&quot;coffee_beans&quot;} DELETE /coffee_beans/:id(.:format) {:action=>&quot;destroy&quot;, :controller=>&quot;coffee_beans&quot;}
  • cups GET /cups(.:format) {:action=>&quot;index&quot;} POST /cups(.:format) {:action=>&quot;create} new_cup GET /cups/new(.:format) {:action=>&quot;new&quot;}edit_cup GET /cups/:id/edit(.:format) {:action=>&quot;edit&quot;} cup GET /cups/:id(.:format) {:action=>&quot;show&quot;} PUT /cups/:id(.:format) {:action=>&quot;update&quot;} DELETE /cups/:id(.:format) {:action=>&quot;destroy} :controller=>&quot;cups&quot;
  • cups GET / cups (.:format) {:action=>&quot; index &quot;} POST / cups (.:format) {:action=>&quot; create } new_cup GET /cups/new(.:format) {:action=>&quot;new&quot;}edit_cup GET /cups/:id/edit(.:format) {:action=>&quot;edit&quot;} cup GET /cups/:id(.:format) {:action=>&quot;show&quot;} PUT /cups/:id(.:format) {:action=>&quot;update&quot;} DELETE /cups/:id(.:format) {:action=>&quot;destroy} :controller=>&quot;cups&quot;
  • cups GET /cups(.:format) {:action=>&quot;index&quot;} POST /cups(.:format) {:action=>&quot;create} new_cup GET /cups/new(.:format) {:action=>&quot;new&quot;}edit_cup GET /cups/:id/edit(.:format) {:action=>&quot;edit&quot;} cup GET / cups/:id (.:format) {:action=>&quot; show &quot;} PUT /cups/:id (.:format) {:action=>&quot; update &quot;} DELETE /cups/:id(.:format) {:action=>&quot;destroy} :controller=>&quot;cups&quot;
  • CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans , :except => :destroy end end
  • CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans , :only => [ :new , :show ] end end
  • CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans , :path_names => [ :edit => ‘brew’ ] end end
  • CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans resource :shop , :except => [ :destroy ] end
  • Singular Routes
  • Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; end Simple routes
  • Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; , :as => &quot;title_search&quot; end Named routes
  • Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#show&quot; , :via => :get end Method-constrained routes
  • Movienight :: Application .routes.draw do get &quot;/search&quot; , :to => &quot;movies#search&quot; , :via => :get end Method-constrained routes
  • Movienight :: Application .routes.draw do root :to => &quot;nights#index&quot; end Root route
  • Non-resourceful Methods
  • CoffeeShop :: Application .routes.draw do resources :coffee_beans do collection do get :dark_roast get :light_roast end member do put :chew end endend end
  • Constraints
  • Movienight :: Application .routes.draw do match &quot;/search&quot; , :constraints => { :subdomain => &quot;mst3k&quot; } end
  • Movienight :: Application .routes.draw do match &quot;/movies/:imdb_id&quot; , :constraints => { :imdb_id => /d+/ } end
  • Movienight :: Application .routes.draw do constraints( :subdomain => &quot;mst3k&quot; ) do resource :projector do resources :bmovies end end end
  • Optional Segments
  • # Rails 2 ActionController :: Routing :: Routes .draw do | map | map.connect &quot;/:controller/:action/:id&quot; end   # Rails 3 Movienight :: Application .routes.draw do match &quot;/:controller(/:action(/:id))(.:format)&quot; end end end
  • Rack Endpoints
  • Rack
  • Movienight :: Application .routes.draw do match &quot;/hello&quot; , :to => Proc .new do | env | [ 200 , { ‘Content-Type’ => ‘text/plain’ }, [ “Hello, world” ] ] end end
  • Movienight :: Application .routes.draw do match &quot;/hello&quot; , :to => Proc .new do | env | [ 200 , { ‘Content-Type’ => ‘text/plain’ }, [ “Hello, world” ] ] end end hello /hello(.:format) {:to=>#<Proc:0x00000100f1da28@/app/config/routes.rb:7>}
  • Sinatra MATT YOHO RAILS 3 FROM A TO Z
  • require 'sinatra/base' class ApiApp < Sinatra :: Base get &quot;/hello&quot; do whom = request.cookies[ &quot;whom&quot; ] whom ||= &quot;world&quot; set_cookie( ‘whom’ , whom) &quot;Hello, #{ whom } &quot; end post &quot;/hello&quot; do set_cookie( ‘whom’ , params [ :whom ]) redirect &quot;/hello&quot; end end
  • Movienight :: Application .routes.draw do scope &quot;/api&quot; do match &quot;(*path)&quot; , :to => ApiApp endend end
  • mount
  • Movienight :: Application .routes.draw do mount ApiApp , :at => &quot;/api&quot; end
  • Special case?
  • Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; end
  • MoviesController .action( :search ) Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; end
  • Movienight :: Application .routes.draw do devise_for :users resources :nights do resources :invitations , :except => [ :index , :destroy ], :path_names => { :edit => 'confirm' } end resources :locations , :except => [ :new , :show ] resources :movies do collection do get :title_search end end root :to => &quot;nights#index&quot; end end
  • Example
  • Movienight
  • Resources
  • http://railsapi.com
  • Rails for Zombies http://railsforzombies.org /
  • Rails 3 Cheat Sheets http://blog.envylabs.com/2010/12/rails-3-cheat-sheets/
  • Rails 3 Upgrade Handbook http://www.railsupgradehandbook.com/
  •