Restful Rails

Embed Size (px)

Citation preview

  • 8/10/2019 Restful Rails

    1/29

    by Olivier AnsaldiRESTful Rails

    http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://flickr.com/photos/tonyapoole/365549081/http://flickr.com/photos/tonyapoole/365549081/http://ozonesoft.net/http://ozonesoft.net/
  • 8/10/2019 Restful Rails

    2/29

    REST

    crashcourse

    http://flickr.com/photos/wvs/62985205/http://flickr.com/photos/wvs/62985205/http://creativecommons.org/licenses/by-nc/3.0/http://creativecommons.org/licenses/by-nc/3.0/
  • 8/10/2019 Restful Rails

    3/29

  • 8/10/2019 Restful Rails

    4/29

    client-server

    stateless

    cache

    uniform interface

    layered system

    code-on-demand

    REST constraints

  • 8/10/2019 Restful Rails

    5/29

    resource

    resource identifier

    representation

    representation metadata

    resource metadata

    control data

    REST data elements

  • 8/10/2019 Restful Rails

    6/29

    REST in Rails

    http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://flickr.com/photos/hiromy/73726954/http://flickr.com/photos/hiromy/73726954/
  • 8/10/2019 Restful Rails

    7/29

    manipulated via HTTP methods

    URL addressable entity

    represented in different formats

    Rails resource

  • 8/10/2019 Restful Rails

    8/29

    Rails resource

    mani ulationsoperation SQL REST

    create insert POST

    read select GET

    update update PUT

    delete delete DELETE

  • 8/10/2019 Restful Rails

    9/29

    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

  • 8/10/2019 Restful Rails

    10/29

    Rails resource

    re resentations

    one controller

    multiple representations

    respond_to

    http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://flickr.com/photos/caitlinburke/182244609/http://flickr.com/photos/caitlinburke/182244609/
  • 8/10/2019 Restful Rails

    11/29

    ./script/generate scaffold_resource \ post \

    title:string \

    content:text \

    created_at:datetime

    Creating a resource

  • 8/10/2019 Restful Rails

    12/29

    Creating a resource

    creates

    model, views, controller, helper

    fixtures, unit tests, functional tests,

    migration (fully functional!)

    modifies

    config/routes.rb

  • 8/10/2019 Restful Rails

    13/29

    Model

    app/models/post.rb

    class Post < ActiveRecord::Base

    end

    nothing new!

    http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://flickr.com/photos/gustavo/84474716/http://flickr.com/photos/gustavo/84474716/
  • 8/10/2019 Restful Rails

    14/29

    Views

    app/views/posts/show.rhtml

    Title:

    Content:

    Created at:

    |

    new syntax for link_toURLs!

  • 8/10/2019 Restful Rails

    15/29

    Views: routes

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

    map.resources :posts

    end

    defines path methods and URL methods

  • 8/10/2019 Restful Rails

    16/29

    Views: routes

    Action HTTP re uest 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

    u date PUT /posts/1 project_path(1)

    delete DELETE /posts/1 project_path(1)

  • 8/10/2019 Restful Rails

    17/29

    Views: routes

    new

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

    edit

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

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

    destroy

    link_to Destroy, post_path(post), :method => :delete

    http://creativecommons.org/licenses/by-nc-nd/3.0/http://creativecommons.org/licenses/by-nc-nd/3.0/http://flickr.com/photos/f-remolo/66576099/http://flickr.com/photos/f-remolo/66576099/
  • 8/10/2019 Restful Rails

    18/29

    Controller

    app/controllers/posts_controller.rb

    def destroy

    @post = Post.find(params[:id]) @post.destroy

    respond_to do |format|

    format.html { redirect_to posts_url }

    format.xml { head :ok }

    end

    end

    uses respond_toand post_url

  • 8/10/2019 Restful Rails

    19/29

    Controller: routes

    Traditional Rails

    redirect_to :controller => posts, \

    :action => show, :id => @post.id

    RESTful Rails

    redirect_to post_url(@post)

    Each path method has equivalent URL method

    Remember: use URL methods for redirect_to

  • 8/10/2019 Restful Rails

    20/29

    Controller: respond_to

    multiple representations of the resource

    respond_touses

    accept HTTP-Header of the request

    format appended to the request URL

    register new formats in config/environment.rbto extend responds_to

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

  • 8/10/2019 Restful Rails

    21/29

    Migration

    db/migrate/001_create_posts.rb

    class 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

    end

    end

    http://flickr.com/photos/boskizzi/82005183/http://flickr.com/photos/boskizzi/82005183/http://creativecommons.org/licenses/by-nc/3.0/http://creativecommons.org/licenses/by-nc/3.0/
  • 8/10/2019 Restful Rails

    22/29

    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

  • 8/10/2019 Restful Rails

    23/29

    Nested resources:

    modelsmodels must be edited to express relationship

    app/models/post.rb

    class Post < ActiveRecord::Base

    has_many :comments

    end

    app/models/comment.rb

    class Comment < ActiveRecord::Base

    belongs_to :post

    end

  • 8/10/2019 Restful Rails

    24/29

    Nested resources:

    routesroutes must be edited to reflect relationship

    config/routes.rbmap.resources :comments

    becomes

    map.resources :posts do |posts| posts.resources :comments

    end

  • 8/10/2019 Restful Rails

    25/29

    Nested resources:

    routesPath 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

  • 8/10/2019 Restful Rails

    26/29

    Nested resources:

    controller

    nested resources controllers must be adapteddef index

    post = Post.find(params[:post_id])

    @comments = post.comments.find(:all)

    ...

    end

  • 8/10/2019 Restful Rails

    27/29

    Thinking in REST terms

    Resources represent

    objects

    relationships

    events

    states

    http://creativecommons.org/licenses/by-nc/3.0/http://creativecommons.org/licenses/by-nc/3.0/http://flickr.com/photos/danw/170440453/http://flickr.com/photos/danw/170440453/
  • 8/10/2019 Restful Rails

    28/29

    REST benefits

    clean URLS

    multiple representations

    less code

    CRUD oriented controllers

    clear application design

    scalability

  • 8/10/2019 Restful Rails

    29/29