0
Advanced RESTful Rails
        Ben Scofield
Constraints
Shall I compare thee to a summer's day?
Thou art more lovely and more temperate.
Rough winds do shake the darling buds of ...
app
   controllers
   helpers
   models
   views
config
   environments
   initializers
db
doc
lib
   tasks
log
public
   ...
exists   app/models/
    exists   app/controllers/
    exists   app/helpers/
    create   app/views/users
    exists   tes...
REST
Audience Participation!
    who is building restful applications?
212,000 Results
How do I handle ...
Difficult
even for the pros
class UsersController < ApplicationController
  # ...

  def activate
    self.current_user = params[:activation_code].bla...
What is REST?
Resources




            hey-helen - flickr
Addressability




                 memestate - flickr
Representations




                  stevedave - flickr
Stateless*




             http://www1.ncdc.noaa.gov/pub/data/images/usa-avhrr.gif
Audience Participation!
         why care?
Process
tiptoe - flickr
Domain


         ejpphoto - flickr
Modeled


          kerim - flickr
?
Identify
 resources
Select
methods to expose
Respect
the middleman
Simple
My Pull List
Releases
ActionController::Routing::Routes.draw do |map|
  map.releases 'releases/:year/:month/:day',
                :controller =...
Issues
ActionController::Routing::Routes.draw do |map|
  map.resources :issues
end




class IssuesController < ApplicationContro...
Series
ActionController::Routing::Routes.draw do |map|
  map.resources :titles
end




class TitlesController < ApplicationContro...
ActionController::Routing::Routes.draw do |map|
  map.resources :titles, :has_many => [:issues]
end




class IssuesContro...
Users
ActionController::Routing::Routes.draw do |map|
  map.resources :users
end




class UsersController < ApplicationControll...
Lists
ActionController::Routing::Routes.draw do |map|
  map.resources :users
end




class UsersController < ApplicationControll...
ActionController::Routing::Routes.draw do |map|
  map.resources :users, :has_many => [:titles]
end




class TitlesControl...
Advanced
Login*   mc - flickr
ActionController::Routing::Routes.draw do |map|
  map.resource :session
end




class SessionsController < ApplicationCont...
Homepage   seandreilinger - flickr
ActionController::Routing::Routes.draw do |map|
  map.resource :homepage
  map.root      :homepage
end




class Homepages...
ActionController::Routing::Routes.draw do |map|
  map.resources :contents
  map.root :controller => ‘contents’, :action =>...
ActionController::Routing::Routes.draw do |map|
  map.resources :ads
  map.root      :ads
end




class AdsController < Ap...
Dashboard   hel2005 - flickr
ActionController::Routing::Routes.draw do |map|
  map.resource :dashboard
end




class DashboardsController < Application...
ActionController::Routing::Routes.draw do |map|
  map.resources :instruments
end




class InstrumentsController < Applica...
Preview   ashoe - flickr
ActionController::Routing::Routes.draw do |map|
  map.resources :posts, :has_one => [:preview]
end




class PreviewsContr...
ActionController::Routing::Routes.draw do |map|
  map.resources :posts
  map.resource :preview
end




class PreviewsContr...
Search   seandreilinger - flickr
ActionController::Routing::Routes.draw do |map|
  map.resources :posts
end




class PostsController < ApplicationControll...
ActionController::Routing::Routes.draw do |map|
  map.resource :search
end




class SearchesController < ApplicationContr...
Wizards   dunechaser - flickr
/galleries/new
/restaurants/:id/photos/new
/restaurants/:id/photos/edit
ActionController::Routing::Routes.draw do |map| map.resources :galleries
  map.resources :galleries
  map.resources :resta...
Collections   wooandy - flickr
Web Services   josefstuefer - flickr
Staff Directory


                                     Inventory
Search Application      Text
                     RESTful...
RESTful API   Staff Directory


                     RESTful API     Inventory
Search Application   Text
                 ...
this slide left intentionally blank




Administration
ActionController::Routing::Routes.draw do |map|
  map.namespace :admin do |admin|
    admin.resources :invitations
    adm...
Audience Participation!
       what gives you fits?
Rails, Specifically
<%= link_to 'Delete', record, :method => 'delete',
                              :confirm => 'Are you sure?' %>




<a hre...
ActionController::Routing::Routes.draw do |map|
  map.users 'users', :controller => ‘users’, :action => ‘index’
  map.user...
ActionController::Routing::Routes.draw do |map|
  map.resources :users

  # Install the default route as the lowest priori...
Collections   wooandy - flickr
ActionController::Routing::Routes.draw do |map|
  map.resources :records
end



class RecordsController < ApplicationContr...
ActionController::Routing::Routes.draw do |map|
  map.resource :record_list
  map.resources :records
end


class RecordLis...
Audience Participation!
      where’s rails bitten you?
Thanks!
ben scofield
ben.scofield@viget.com
http://www.viget.com/extend
http://www.culann.com
Advanced RESTful Rails
Advanced RESTful Rails
Upcoming SlideShare
Loading in...5
×

Advanced RESTful Rails

3,224

Published on

Slides from Ben Scofield's session on Advanced RESTful Rails at Railsconf 2008

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,224
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
108
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "Advanced RESTful Rails"

  1. 1. Advanced RESTful Rails Ben Scofield
  2. 2. Constraints
  3. 3. Shall I compare thee to a summer's day? Thou art more lovely and more temperate. Rough winds do shake the darling buds of May, And summer's lease hath all too short a date. Sometime too hot the eye of heaven shines, And often is his gold complexion dimm'd; And every fair from fair some time declines, By chance, or nature's changing course, untrimm'd; But thy eternal summer shall not fade ...
  4. 4. app controllers helpers models views config environments initializers db doc lib tasks log public images javascripts stylesheets script performance process test fixtures functional integration unit ...
  5. 5. exists app/models/ exists app/controllers/ exists app/helpers/ create app/views/users exists test/functional/ exists test/unit/ dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/user.rb create test/unit/user_test.rb create test/fixtures/users.yml create db/migrate create db/migrate/20080531002035_create_users.rb create app/controllers/users_controller.rb create test/functional/users_controller_test.rb create app/helpers/users_helper.rb route map.resources :users
  6. 6. REST
  7. 7. Audience Participation! who is building restful applications?
  8. 8. 212,000 Results
  9. 9. How do I handle ...
  10. 10. Difficult even for the pros
  11. 11. class UsersController < ApplicationController # ... def activate self.current_user = params[:activation_code].blank? ? false : # ... if logged_in? && !current_user.active? current_user.activate! flash[:notice] = quot;Signup complete!quot; end redirect_back_or_default('/') end def suspend @user.suspend! redirect_to users_path end def unsuspend @user.unsuspend! redirect_to users_path end def destroy @user.delete! redirect_to users_path end def purge @user.destroy redirect_to users_path Restful Authentication end end
  12. 12. What is REST?
  13. 13. Resources hey-helen - flickr
  14. 14. Addressability memestate - flickr
  15. 15. Representations stevedave - flickr
  16. 16. Stateless* http://www1.ncdc.noaa.gov/pub/data/images/usa-avhrr.gif
  17. 17. Audience Participation! why care?
  18. 18. Process
  19. 19. tiptoe - flickr
  20. 20. Domain ejpphoto - flickr
  21. 21. Modeled kerim - flickr
  22. 22. ?
  23. 23. Identify resources
  24. 24. Select methods to expose
  25. 25. Respect the middleman
  26. 26. Simple
  27. 27. My Pull List
  28. 28. Releases
  29. 29. ActionController::Routing::Routes.draw do |map| map.releases 'releases/:year/:month/:day', :controller => 'items', :action => 'index' end class ItemsController < ApplicationController # release listing page; filters on year/month/day from params def index; end end
  30. 30. Issues
  31. 31. ActionController::Routing::Routes.draw do |map| map.resources :issues end class IssuesController < ApplicationController # issue detail page def show; end end
  32. 32. Series
  33. 33. ActionController::Routing::Routes.draw do |map| map.resources :titles end class TitlesController < ApplicationController # title detail page def show; end end
  34. 34. ActionController::Routing::Routes.draw do |map| map.resources :titles, :has_many => [:issues] end class IssuesController < ApplicationController # issue listing page; could be series page def index; end end
  35. 35. Users
  36. 36. ActionController::Routing::Routes.draw do |map| map.resources :users end class UsersController < ApplicationController before_filter :require_login, :only => [:edit, :update] # edit account def edit; end # update account def update; end end
  37. 37. Lists
  38. 38. ActionController::Routing::Routes.draw do |map| map.resources :users end class UsersController < ApplicationController # public view - pull list def show; end end
  39. 39. ActionController::Routing::Routes.draw do |map| map.resources :users, :has_many => [:titles] end class TitlesController < ApplicationController # public view - pull list, given a user_id def index; end end
  40. 40. Advanced
  41. 41. Login* mc - flickr
  42. 42. ActionController::Routing::Routes.draw do |map| map.resource :session end class SessionsController < ApplicationController # login form def new; end # login action def create; end # logout action def destroy; end end
  43. 43. Homepage seandreilinger - flickr
  44. 44. ActionController::Routing::Routes.draw do |map| map.resource :homepage map.root :homepage end class HomepagesController < ApplicationController # homepage def show; end end
  45. 45. ActionController::Routing::Routes.draw do |map| map.resources :contents map.root :controller => ‘contents’, :action => ‘show’, :page => ‘homepage’ end class ContentsController < ApplicationController # static content def show # code to find a template named according to params[:page] end end
  46. 46. ActionController::Routing::Routes.draw do |map| map.resources :ads map.root :ads end class AdsController < ApplicationController # ad index - the million dollar homepage def index; end end
  47. 47. Dashboard hel2005 - flickr
  48. 48. ActionController::Routing::Routes.draw do |map| map.resource :dashboard end class DashboardsController < ApplicationController before_filter :require_login # dashboard def show; end end
  49. 49. ActionController::Routing::Routes.draw do |map| map.resources :instruments end class InstrumentsController < ApplicationController before_filter :require_login # dashboard def index @instruments = current_user.instruments end end
  50. 50. Preview ashoe - flickr
  51. 51. ActionController::Routing::Routes.draw do |map| map.resources :posts, :has_one => [:preview] end class PreviewsController < ApplicationController def create @post = Post.find(params[:post_id]) @post.attributes = params[:post] render :template => 'posts/show' end end
  52. 52. ActionController::Routing::Routes.draw do |map| map.resources :posts map.resource :preview end class PreviewsController < ApplicationController def create @post = Post.new(params[:post]) render :template => 'posts/show' end end
  53. 53. Search seandreilinger - flickr
  54. 54. ActionController::Routing::Routes.draw do |map| map.resources :posts end class PostsController < ApplicationController def index if params[:query].blank? @posts = Post.find(:all) else @posts = Post.find_for_query(params[:query]) end end end
  55. 55. ActionController::Routing::Routes.draw do |map| map.resource :search end class SearchesController < ApplicationController def show @results = Searcher.find(params[:query]) end end
  56. 56. Wizards dunechaser - flickr
  57. 57. /galleries/new
  58. 58. /restaurants/:id/photos/new
  59. 59. /restaurants/:id/photos/edit
  60. 60. ActionController::Routing::Routes.draw do |map| map.resources :galleries map.resources :galleries map.resources :restaurants, :has_many => [:photos] map.with_options :controller => 'photos' do |p| p.edit_restaurant_photos 'restaurants/:restaurant_id/photos/edit', :action => 'edit' p.update_restaurant_photos 'restaurants/:restaurant_id/photos/update', :action => 'update', :conditions => {:method => :put} end end
  61. 61. Collections wooandy - flickr
  62. 62. Web Services josefstuefer - flickr
  63. 63. Staff Directory Inventory Search Application Text RESTful API HR etc.
  64. 64. RESTful API Staff Directory RESTful API Inventory Search Application Text RESTful API HR RESTful API etc.
  65. 65. this slide left intentionally blank Administration
  66. 66. ActionController::Routing::Routes.draw do |map| map.namespace :admin do |admin| admin.resources :invitations admin.resources :emails admin.resources :users admin.resources :features end end
  67. 67. Audience Participation! what gives you fits?
  68. 68. Rails, Specifically
  69. 69. <%= link_to 'Delete', record, :method => 'delete', :confirm => 'Are you sure?' %> <a href=quot;/records/1quot; onclick=quot;if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;quot;>Delete</a> Accessibility
  70. 70. ActionController::Routing::Routes.draw do |map| map.users 'users', :controller => ‘users’, :action => ‘index’ map.users 'users', :controller => ‘users’, :action => ‘create’ end Hand-Written Routes
  71. 71. ActionController::Routing::Routes.draw do |map| map.resources :users # Install the default route as the lowest priority. map.connect ':controller/:action/:id' end Default Routing
  72. 72. Collections wooandy - flickr
  73. 73. ActionController::Routing::Routes.draw do |map| map.resources :records end class RecordsController < ApplicationController def index; end def show; end # ... end Mixed
  74. 74. ActionController::Routing::Routes.draw do |map| map.resource :record_list map.resources :records end class RecordListsController < ApplicationController def show; end # ... end class RecordsController < ApplicationController def show; end # ... end Separated
  75. 75. Audience Participation! where’s rails bitten you?
  76. 76. Thanks! ben scofield ben.scofield@viget.com http://www.viget.com/extend http://www.culann.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×