• Like
  • Save
Rails 3 from A to Z
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Rails 3 from A to Z

  • 2,144 views
Published

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

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

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,144
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • 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/

Transcript

  • 1.  
  • 2.  
  • 3. @speaker = { :name => ‘Matt Yoho’ , :company => EdgeCase , :writes => [ ‘Ruby on Rails’ ], :loves => [ ‘Comic Books’ ] }
  • 4. Goals
  • 5. Rails is a Domain Specific Language for web applications written in and on top of Ruby.
  • 6. Rails 1.0 December 2005
  • 7. Rails 2.0 December 2007
  • 8. Rails 2.3 March 2009
  • 9. Rails 3 .0 August 2010
  • 10. Rails 3 .0.3 November 2010
  • 11. Agenda
  • 12. 1) Rails Overview
  • 13. 2) Highlight new features
  • 14. 3) Example app
  • 15. Rails concepts
  • 16. MVC
  • 17. M odel VC
  • 18. MV iew C
  • 19. MV C ontroller
  • 20. Models Views Controllers Model View Controller
  • 21. Models Views Controllers Helpers Model View Controller
  • 22. Models Views Controllers Routes Helpers Model View Controller
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. < 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
  • 27. View Nested structure. Layouts Templates Partials Partials
  • 28. View Contain < head > and < body > tags, basic stylesheets and JavaScript links. One or a few per app. Layouts
  • 29. View Templates Contain page-specific markup and exist for approximately each controller action.
  • 30. View Small, reusable template components. Partials Partials
  • 31. 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 )
  • 32. 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
  • 33. 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.
  • 34. 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.
  • 35. 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
  • 36. DRY
  • 37. Don’t Repeat Yourself
  • 38. 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
  • 39. 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
  • 40. ConventionConfiguration
  • 41. REST
  • 42. Define APIs
  • 43. Define RESTful APIs
  • 44. Resources
  • 45. Identified by URIs
  • 46. http://movienight.com/movies/123
  • 47. Movie id = 123 ... http://movienight.com/movies/123
  • 48. May be nested
  • 49. http://movienight.com/movies/123/actors/4
  • 50. Movie id = 123 http://movienight.com/movies/123/actors/4 Actor id = 4
  • 51. May have multiple representations
  • 52. http://movienight.com/movies/123.json
  • 53. 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}
  • 54. http://movienight.com/movies/123.xml
  • 55. 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>
  • 56. Rely on HTTP content type negotiation
  • 57. HTTP headers
  • 58. HTTP headers http://movienight.com/movies/123.json
  • 59. Accept: application/json, text/javascript, */* ; Request header
  • 60. Content-Type: application/json; Response header
  • 61. CRUD via HTTP VERBS
  • 62. GET POST PUT DELETE HTTP verbs
  • 63. READ CREATE UPDATE DELETE CRUD operations
  • 64. SHOW CREATE UPDATE DESTROY Rails controller actions
  • 65. SHOW CREATE UPDATE DESTROY NEW EDIT INDEX
  • 66. GET /movies/123 HTTP/1.1 Accept:text/html
  • 67. GET /movies/123 HTTP/1.1 Accept:text/html CRUD op
  • 68. GET /movies/123 HTTP/1.1 Accept:text/html Resource
  • 69. GET /movies/123 HTTP/1.1 Accept:text/html MoviesController#show
  • 70. Strongly encouraged by Rails
  • 71. Request -> Response Cycle
  • 72. Models Views Controllers Routes Request-response cycle Client
  • 73. Models Views Controllers Routes Request-response cycle Client
  • 74. Models Views Controllers Routes Request-response cycle Client
  • 75. Models Views Controllers Routes Request-response cycle Client
  • 76. Models Views Controllers Routes Request-response cycle Client
  • 77. Models Views Controllers Routes Request-response cycle Client
  • 78. Models Views Controllers Routes Request-response cycle Client
  • 79. Models Views Controllers Routes Request-response cycle Client
  • 80. Models Views Controllers Routes Request-response cycle Client Webpage!
  • 81. Feature details
  • 82.  
  • 83.  
  • 84. Bundler
  • 85. Commands
  • 86. rails new ~/app/name
  • 87. rails new ~/app/name -T -J
  • 88. rails new ~/app/name -T -J -T = Skip Test::Unit
  • 89. rails new ~/app/name -T -J -J = Skip Prototype
  • 90. rails generate < thing >
  • 91. rails server
  • 92. rails console
  • 93. Dependency Management
  • 94. can't activate cucumber (= 0.4.4, runtime) for [], already activated cucumber-0.6.1 for [] (Gem::LoadError)
  • 95. $ 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
  • 96. $ 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
  • 97. $ 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
  • 98. Gemfile
  • 99. 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
  • 100. source 'http://rubygems.org'
  • 101. gem 'rails' , '~> 3.0.3'
  • 102. gem 'rails' , '~> 3.0.3' Name
  • 103. gem 'rails' , '~> 3.0.3' Version
  • 104. :mri :mri_18 :mri_19 :mswin :jruby Platforms
  • 105. gem 'ruby-debug' , :platforms => :mri_18
  • 106. gem 'ruby-debug19' , :platforms => :mri_19
  • 107. Install from Git
  • 108. gem 'basic_assumption' , :git => 'git://github.com/mattyoho/basic_assumption.git' , :branch => 'experimental'
  • 109. gem 'basic_assumption' , :git => 'git://github.com/mattyoho/basic_assumption.git' , :branch => 'experimental' GitHub repo
  • 110. gem 'basic_assumption' , :git => 'git://github.com/mattyoho/basic_assumption.git' , :branch => 'experimental' Git repo branch
  • 111. bundle install
  • 112. group :test do gem 'rspec-rails' gem 'cucumber' gem 'fakeweb' end end
  • 113. bundle install --without test
  • 114. bundle open < gem name >
  • 115. bundle open < gem name >
  • 116. bundle update
  • 117. bundle package
  • 118. http://gembundler.org/ http://gembundler.com/man/gemfile.5.html
  • 119. MATT YOHO RAILS 3 FROM A TO Z Models MATT YOHO RAILS 3 FROM A TO Z
  • 120. The great split
  • 121. ActiveRecord MATT YOHO RAILS 3 FROM A TO Z
  • 122. ActiveRecord ActiveModel ActiveRelation
  • 123. ActiveModel
  • 124. ActiveModel :: Validations ActiveModel :: Callbacks ActiveModel :: Errors ActiveModel :: Dirty
  • 125. validates :title , :presence => true :format => /[a-zA-Z]/
  • 126. ActiveModel :: Lint
  • 127. Mongoid Datamapper
  • 128. ActiveRelation
  • 129. Not as speedy?
  • 130. @tenderlove Comeplete rewrite of Arel
  • 131. Movie . select( ‘*’ ) where( “title ILIKE %batman%” ). order( ‘release DESC’ )
  • 132. criteria = Movie . select( ‘*’ ) where( “title ILIKE %batman%” ). order( ‘release DESC’ )
  • 133. criteria = Movie . select( ‘*’ ) where( “title ILIKE %batman%” ). order( ‘release DESC’ ) criteria.to_sql
  • 134. &quot;SELECT * FROM &quot;movies&quot; WHERE (title ILIKE '%batman%') ORDER BY title&quot;
  • 135. select() where() order() joins() includes() limit() group() having() etc.
  • 136. named_scope
  • 137. scope
  • 138. scope :batman_flicks , where( “title ILIKE %batman%” )
  • 139. default_scope order( “title” )
  • 140. MATT YOHO RAILS 3 FROM A TO Z Controllers
  • 141. MATT YOHO RAILS 3 FROM A TO Z ActionController respond_to
  • 142. 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
  • 143. MATT YOHO RAILS 3 FROM A TO Z ActionController respond_with
  • 144. 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
  • 145. 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
  • 146. 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
  • 147. MATT YOHO RAILS 3 FROM A TO Z mail gem
  • 148. MATT YOHO RAILS 3 FROM A TO Z ActionMailer :: Base AbstractController::Base ActionController :: Base
  • 149. Views
  • 150. Whitelist, not blacklist
  • 151. <%= h ( @movie .description) %>
  • 152. <%= @movie .description %>
  • 153. <%= raw( “<h2> #{ @movie .title } </h2>” ) %>
  • 154. <%= raw( “<h2> #{ @movie .title } </h2>” ) %> String #html_safe
  • 155. Unobtrusive JS
  • 156. link_to_remote &quot;Destroy&quot; , :url => movie_url( @movie ), :method => :delete
  • 157. <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
  • 158. link_to &quot;Destroy&quot; , movie_url( @movie ), :remote => true , :method => :delete
  • 159. <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
  • 160. https://github.com/rails/jquery-ujs
  • 161. Routing
  • 162. Pneumatic tubes, popular in the late 19th and early 20th century for transporting messages, cash, etc.
  • 163. Text Resources
  • 164. CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans end end
  • 165. 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;}
  • 166. 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;
  • 167. 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;
  • 168. 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;
  • 169. CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans , :except => :destroy end end
  • 170. CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans , :only => [ :new , :show ] end end
  • 171. CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans , :path_names => [ :edit => ‘brew’ ] end end
  • 172. CoffeeShop :: Application .routes.draw do resources :cups do resources :shots end resources :coffee_beans resource :shop , :except => [ :destroy ] end
  • 173. Singular Routes
  • 174. Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; end Simple routes
  • 175. Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; , :as => &quot;title_search&quot; end Named routes
  • 176. Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#show&quot; , :via => :get end Method-constrained routes
  • 177. Movienight :: Application .routes.draw do get &quot;/search&quot; , :to => &quot;movies#search&quot; , :via => :get end Method-constrained routes
  • 178. Movienight :: Application .routes.draw do root :to => &quot;nights#index&quot; end Root route
  • 179. Non-resourceful Methods
  • 180. 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
  • 181. Constraints
  • 182. Movienight :: Application .routes.draw do match &quot;/search&quot; , :constraints => { :subdomain => &quot;mst3k&quot; } end
  • 183. Movienight :: Application .routes.draw do match &quot;/movies/:imdb_id&quot; , :constraints => { :imdb_id => /d+/ } end
  • 184. Movienight :: Application .routes.draw do constraints( :subdomain => &quot;mst3k&quot; ) do resource :projector do resources :bmovies end end end
  • 185. Optional Segments
  • 186. # 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
  • 187. Rack Endpoints
  • 188. Rack
  • 189. Movienight :: Application .routes.draw do match &quot;/hello&quot; , :to => Proc .new do | env | [ 200 , { ‘Content-Type’ => ‘text/plain’ }, [ “Hello, world” ] ] end end
  • 190. 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>}
  • 191. Sinatra MATT YOHO RAILS 3 FROM A TO Z
  • 192. 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
  • 193. Movienight :: Application .routes.draw do scope &quot;/api&quot; do match &quot;(*path)&quot; , :to => ApiApp endend end
  • 194. mount
  • 195. Movienight :: Application .routes.draw do mount ApiApp , :at => &quot;/api&quot; end
  • 196. Special case?
  • 197. Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; end
  • 198. MoviesController .action( :search ) Movienight :: Application .routes.draw do match &quot;/search&quot; , :to => &quot;movies#search&quot; end
  • 199. 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
  • 200. Example
  • 201. Movienight
  • 202. Resources
  • 203. http://railsapi.com
  • 204. Rails for Zombies http://railsforzombies.org /
  • 205. Rails 3 Cheat Sheets http://blog.envylabs.com/2010/12/rails-3-cheat-sheets/
  • 206. Rails 3 Upgrade Handbook http://www.railsupgradehandbook.com/
  • 207.