29
by Olivier Ansaldi http://ozonesoft.net/ RESTful Rails Picture by Tonya Poole - http://flickr.com/photos/tonyapoole/365549081/ This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License

RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

by Olivier Ansaldi

http://ozonesoft.net/

RESTful Rails

Picture by Tonya Poole - http://flickr.com/photos/tonyapoole/365549081/

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License

Page 3: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

coined by Roy Fielding

stands for REpresentational State Transfer

describes an architecture for web apps centered around resources

What is REST?

Page 4: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

client-server

stateless

cache

uniform interface

layered system

code-on-demand

REST constraints

Page 5: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

resource

resource identifier

representation

representation metadata

resource metadata

control data

REST data elements

Page 7: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

manipulated via HTTP methods

URL addressable entity

represented in different formats

Rails resource

Page 8: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Rails resource manipulations

operation SQL REST

create insert POST

read select GET

update update PUT

delete delete DELETE

Page 9: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Rails resource URLs

Traditional Rails RESTful Rails

POST /post/create POST /post

GET /post/show/1 GET /post/1

POST /post/update/1 PUT /post/1

POST /post/destroy/1 DELETE /post/1

Page 10: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Rails resource representations

one controller

multiple representations

respond_to

Picture by Caitlin Burke - http://flickr.com/photos/caitlinburke/182244609/

Page 11: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

./script/generate scaffold_resource \ post \ title:string \ content:text \ created_at:datetime

Creating a resource

Page 12: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Creating a resource

creates

model, views, controller, helper

fixtures, unit tests, functional tests,

migration (fully functional!)

modifies

config/routes.rb

Page 13: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Model

app/models/post.rbclass Post < ActiveRecord::Baseend

nothing new!

Picture by Gustavo Marin - http://flickr.com/photos/gustavo/84474716/

Page 14: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Views

app/views/posts/show.rhtml<p><b>Title:</b><%=h @post.title %></b></p><p><b>Content:</b><%=h @post.content %></b></p><p><b>Created at:</b><%=h @post.created_at %></b></p>

<%= link_to ‘Edit’, edit_post_path(@post) %> |<%= link_to ‘Back’, posts_path %>

new syntax for link_to URLs!

Page 15: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Views: routes

config/routes.rbActionController::Routing::Routes.draw do |map| map.resources :postsend

defines path methods and URL methods

Page 16: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Views: routes

Action HTTP request Path methodindex GET /posts projects_path

show GET /posts/1 project_path(1)

new GET /posts/new new_project_path

edit GET /posts/1;edit edit_project_path(1)

create POST /posts projects_path

update PUT /posts/1 project_path(1)

delete DELETE /posts/1 project_path(1)

Page 17: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Views: routes

new

form_for(:post, :url => post_path) ...

editform_for(:post, :url => post_path(@post), \

:html => {:method => :put}) ...

destroylink_to ‘Destroy’, post_path(post), :method => :delete

Picture by Francesco Remolo - http://flickr.com/photos/f-remolo/66576099/

Page 18: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Controller

app/controllers/posts_controller.rbdef destroy @post = Post.find(params[:id]) @post.destroy

respond_to do |format| format.html { redirect_to posts_url } format.xml { head :ok } endend

uses respond_to and post_url

Page 19: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Controller: routes

Traditional Railsredirect_to :controller => ‘posts’, \ :action => ‘show’, :id => @post.id

RESTful Railsredirect_to post_url(@post)

Each path method has equivalent URL method

Remember: use URL methods for redirect_to

Page 20: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Controller: respond_to

multiple representations of the resource

respond_to uses

accept HTTP-Header of the request

format appended to the request URL

register new formats in config/environment.rb to extend responds_to

Mime::Type.register ‘image/png’, :png

Page 21: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Migration

db/migrate/001_create_posts.rbclass CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.column :title, :string t.column :content, :text t.column :created_at, :datetime end end

def self.down drop_table :posts endend

Picture by Max Boschini - http://flickr.com/photos/boskizzi/82005183/

Page 22: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Nested resources

strongly coupled resources

expressed in the URLs

creating a nested resource./script/generate scaffold_resource comment \ post_id:integer created_at:datetime \ author:string content:text

Page 23: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Nested resources: models

models must be edited to express relationship

app/models/post.rbclass Post < ActiveRecord::Base has_many :commentsend

app/models/comment.rbclass Comment < ActiveRecord::Base belongs_to :postend

Page 24: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Nested resources: routes

routes must be edited to reflect relationship

config/routes.rbmap.resources :comments

becomesmap.resources :posts do |posts| posts.resources :commentsend

Page 25: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Nested resources: routes

Path method Path

comments_path(1) /posts/1/comments

comment_path(1, 2) /posts/1/comments/2

new_comment_path(1) /posts/1/comments/new

edit_comment_path(1) /posts/1/comments/2;edit

Page 26: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Nested resources: controller

nested resources controllers must be adapteddef index post = Post.find(params[:post_id]) @comments = post.comments.find(:all) ...end

Page 27: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

Thinking in REST terms

Resources represent

objects

relationships

events

states

Picture by Dan Woods - http://flickr.com/photos/danw/170440453/

Page 28: RESTful Rails - WordPress.com · coined by Roy Fielding stands for REpresentational State Transfer describes an architecture for web apps centered around resources What is REST?

REST benefits

clean URLS

multiple representations

less code

CRUD oriented controllers

clear application design

scalability