Advanced RESTful Rails Guide
Advanced RESTful Rails Guide
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 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
...
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
...
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/[Link]
create test/unit/user_test.rb
create test/fixtures/[Link]
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 [Link] :users
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].blank? ? false : # ...
if logged_in? && !current_user.active?
current_user.activate!
flash[:notice] = "Signup complete!"
end
redirect_back_or_default('/')
end
def suspend
@[Link]!
redirect_to users_path
end
def unsuspend
@[Link]!
redirect_to users_path
end
def destroy
@[Link]!
redirect_to users_path
end
def purge
@[Link]
redirect_to users_path
Restful Authentication
end
end
What is REST?
Resources
hey-helen - flickr
Addressability
memestate - flickr
Representations
stevedave - flickr
Stateless*
[Link]
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::[Link] do |map|
[Link] 'releases/:year/:month/:day',
:controller => 'items', :action => 'index'
end
# edit account
def edit; end
# update account
def update; end
end
Lists
ActionController::Routing::[Link] do |map|
[Link] :users
end
# login action
def create; end
# logout action
def destroy; end
end
Homepage seandreilinger - flickr
ActionController::Routing::[Link] do |map|
[Link] :homepage
[Link] :homepage
end
# dashboard
def show; end
end
ActionController::Routing::[Link] do |map|
[Link] :instruments
end
# dashboard
def index
@instruments = current_user.instruments
end
end
Preview ashoe - flickr
ActionController::Routing::[Link] do |map|
[Link] :posts, :has_one => [:preview]
end
Inventory
Search Application Text
RESTful API
HR
etc.
RESTful API Staff Directory
Administration
ActionController::Routing::[Link] do |map|
[Link] :admin do |admin|
[Link] :invitations
[Link] :emails
[Link] :users
[Link] :features
end
end
Audience Participation!
what gives you fits?
Rails, Specifically
<%= link_to 'Delete', record, :method => 'delete',
:confirm => 'Are you sure?' %>
Accessibility
ActionController::Routing::[Link] do |map|
[Link] 'users', :controller => ‘users’, :action => ‘index’
[Link] 'users', :controller => ‘users’, :action => ‘create’
end
Hand-Written Routes
ActionController::Routing::[Link] do |map|
[Link] :users
Default Routing
Collections wooandy - flickr
ActionController::Routing::[Link] do |map|
[Link] :records
end
# ...
end
Mixed
ActionController::Routing::[Link] do |map|
[Link] :record_list
[Link] :records
end
# ...
end
# ...
end
Separated
Audience Participation!
where’s rails bitten you?
Thanks!
ben scofield
[Link]@[Link]
[Link]
[Link]