Download - Advanced RESTful Rails
![Page 1: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/1.jpg)
Advanced RESTful RailsBen Scofield
![Page 2: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/2.jpg)
Constraints
![Page 3: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/3.jpg)
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...
![Page 4: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/4.jpg)
![Page 5: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/5.jpg)
app controllers helpers models viewsconfig environments initializersdbdoclib taskslogpublic images javascripts stylesheetsscript performance processtest fixtures functional integration unit...
![Page 6: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/6.jpg)
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
![Page 7: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/7.jpg)
REST
![Page 8: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/8.jpg)
Audience Participation!who is building restful applications?
![Page 9: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/9.jpg)
212,000 Results
![Page 10: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/10.jpg)
How do I handle ...
![Page 11: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/11.jpg)
Difficulteven for the pros
![Page 12: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/12.jpg)
class UsersController < ApplicationController # ...
def activate self.current_user = params[:activation_code].blank? ? false : # ... if logged_in? && !current_user.active? current_user.activate! flash[:notice] = "Signup complete!" 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 endendRestful Authentication
![Page 13: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/13.jpg)
What is REST?
![Page 14: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/14.jpg)
Resources
hey-helen - flickr
![Page 15: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/15.jpg)
Addressability
memestate - flickr
![Page 16: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/16.jpg)
Representations
stevedave - flickr
![Page 17: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/17.jpg)
Stateless*
http://www1.ncdc.noaa.gov/pub/data/images/usa-avhrr.gif
![Page 18: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/18.jpg)
Audience Participation!why care?
![Page 19: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/19.jpg)
Process
![Page 20: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/20.jpg)
tiptoe - flickr
![Page 21: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/21.jpg)
Domain
ejpphoto - flickr
![Page 22: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/22.jpg)
Modeled
kerim - flickr
![Page 23: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/23.jpg)
?
![Page 24: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/24.jpg)
Identifyresources
![Page 25: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/25.jpg)
Selectmethods to expose
![Page 26: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/26.jpg)
Respectthe middleman
![Page 27: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/27.jpg)
Simple
![Page 28: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/28.jpg)
My Pull List
![Page 29: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/29.jpg)
Releases
![Page 30: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/30.jpg)
class ItemsController < ApplicationController # release listing page; filters on year/month/day from params def index; endend
ActionController::Routing::Routes.draw do |map| map.releases 'releases/:year/:month/:day', :controller => 'items', :action => 'index'end
![Page 31: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/31.jpg)
Issues
![Page 32: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/32.jpg)
class IssuesController < ApplicationController # issue detail page def show; endend
ActionController::Routing::Routes.draw do |map| map.resources :issuesend
![Page 33: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/33.jpg)
Series
![Page 34: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/34.jpg)
class TitlesController < ApplicationController # title detail page def show; endend
ActionController::Routing::Routes.draw do |map| map.resources :titlesend
![Page 35: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/35.jpg)
class IssuesController < ApplicationController # issue listing page; could be series page def index; endend
ActionController::Routing::Routes.draw do |map| map.resources :titles, :has_many => [:issues]end
![Page 36: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/36.jpg)
Users
![Page 37: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/37.jpg)
class UsersController < ApplicationController before_filter :require_login, :only => [:edit, :update]
# edit account def edit; end
# update account def update; endend
ActionController::Routing::Routes.draw do |map| map.resources :usersend
![Page 38: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/38.jpg)
Lists
![Page 39: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/39.jpg)
class UsersController < ApplicationController # public view - pull list def show; endend
ActionController::Routing::Routes.draw do |map| map.resources :usersend
![Page 40: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/40.jpg)
class TitlesController < ApplicationController # public view - pull list, given a user_id def index; endend
ActionController::Routing::Routes.draw do |map| map.resources :users, :has_many => [:titles]end
![Page 41: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/41.jpg)
Advanced
![Page 42: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/42.jpg)
Login*mc - flickr
![Page 43: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/43.jpg)
class SessionsController < ApplicationController # login form def new; end
# login action def create; end
# logout action def destroy; endend
ActionController::Routing::Routes.draw do |map| map.resource :sessionend
![Page 44: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/44.jpg)
Homepageseandreilinger - flickr
![Page 45: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/45.jpg)
class HomepagesController < ApplicationController # homepage def show; endend
ActionController::Routing::Routes.draw do |map| map.resource :homepage map.root :homepageend
![Page 46: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/46.jpg)
class ContentsController < ApplicationController # static content def show # code to find a template named according to params[:page] endend
ActionController::Routing::Routes.draw do |map| map.resources :contents map.root :controller => ‘contents’, :action => ‘show’, :page => ‘homepage’end
![Page 47: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/47.jpg)
class AdsController < ApplicationController # ad index - the million dollar homepage def index; endend
ActionController::Routing::Routes.draw do |map| map.resources :ads map.root :adsend
![Page 48: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/48.jpg)
Dashboardhel2005 - flickr
![Page 49: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/49.jpg)
class DashboardsController < ApplicationController before_filter :require_login
# dashboard def show; endend
ActionController::Routing::Routes.draw do |map| map.resource :dashboardend
![Page 50: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/50.jpg)
class InstrumentsController < ApplicationController before_filter :require_login
# dashboard def index @instruments = current_user.instruments endend
ActionController::Routing::Routes.draw do |map| map.resources :instrumentsend
![Page 51: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/51.jpg)
Previewashoe - flickr
![Page 52: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/52.jpg)
class PreviewsController < ApplicationController def create @post = Post.find(params[:post_id]) @post.attributes = params[:post]
render :template => 'posts/show' endend
ActionController::Routing::Routes.draw do |map| map.resources :posts, :has_one => [:preview]end
![Page 53: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/53.jpg)
class PreviewsController < ApplicationController def create @post = Post.new(params[:post])
render :template => 'posts/show' endend
ActionController::Routing::Routes.draw do |map| map.resources :posts map.resource :previewend
![Page 54: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/54.jpg)
Searchseandreilinger - flickr
![Page 55: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/55.jpg)
class PostsController < ApplicationController def index if params[:query].blank? @posts = Post.find(:all) else @posts = Post.find_for_query(params[:query]) end endend
ActionController::Routing::Routes.draw do |map| map.resources :postsend
![Page 56: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/56.jpg)
class SearchesController < ApplicationController def show @results = Searcher.find(params[:query]) endend
ActionController::Routing::Routes.draw do |map| map.resource :searchend
![Page 57: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/57.jpg)
Wizardsdunechaser - flickr
![Page 58: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/58.jpg)
![Page 59: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/59.jpg)
/galleries/new
![Page 60: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/60.jpg)
/restaurants/:id/photos/new
![Page 61: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/61.jpg)
/restaurants/:id/photos/edit
![Page 62: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/62.jpg)
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} endend
![Page 63: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/63.jpg)
Collectionswooandy - flickr
![Page 64: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/64.jpg)
Web Servicesjosefstuefer - flickr
![Page 65: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/65.jpg)
Text
Inventory
Staff Directory
HR
etc.
RESTful APISearch Application
![Page 66: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/66.jpg)
Text
Inventory
Staff Directory
HR
etc.
RESTful API
RESTful API
RESTful API
RESTful API
Search Application
![Page 67: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/67.jpg)
Administration
this slide left intentionally blank
![Page 68: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/68.jpg)
ActionController::Routing::Routes.draw do |map| map.namespace :admin do |admin| admin.resources :invitations admin.resources :emails admin.resources :users admin.resources :features endend
![Page 69: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/69.jpg)
Audience Participation!what gives you fits?
![Page 70: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/70.jpg)
Rails, Specifically
![Page 71: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/71.jpg)
<a href="/records/1" onclick="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;">Delete</a>
<%= link_to 'Delete', record, :method => 'delete', :confirm => 'Are you sure?' %>
Accessibility
![Page 72: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/72.jpg)
ActionController::Routing::Routes.draw do |map| map.users 'users', :controller => ‘users’, :action => ‘index’ map.users 'users', :controller => ‘users’, :action => ‘create’ end
Hand-Written Routes
![Page 73: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/73.jpg)
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
![Page 74: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/74.jpg)
Collectionswooandy - flickr
![Page 75: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/75.jpg)
class RecordsController < ApplicationController def index; end
def show; end
# ...end
Mixed
ActionController::Routing::Routes.draw do |map| map.resources :recordsend
![Page 76: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/76.jpg)
Separated
ActionController::Routing::Routes.draw do |map| map.resource :record_list map.resources :recordsend
class RecordListsController < ApplicationController def show; end
# ...end
class RecordsController < ApplicationController def show; end
# ...end
![Page 77: Advanced RESTful Rails](https://reader038.vdocuments.us/reader038/viewer/2022103016/555844bed8b42acb078b52c2/html5/thumbnails/77.jpg)
Audience Participation!where’s rails bitten you?