Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Putting CRUD to REST: An Introduction to RESTful Routing by Dan Berger

Putting CRUD to REST: An Introduction to RESTful Routing by Dan Berger

Viking Education

July 17, 2015
Tweet

More Decks by Viking Education

Other Decks in Technology

Transcript

  1. Goals - Briefly introduce you to Representational State Transfer, or

    REST. - Give you a mental model for relating RESTful controller actions to CRUD principles.
  2. Read: Index & Show actions - Index reads all the

    instances of this object. - @foos = Foo.all - Show reads one instance of this object. - @foo = Foo.find(params[:id]) - They are both HTML ‘get’ actions.
  3. Create: New controller action - HTML ‘get’ request from browser

    - Controller creates an empty object: - @foo = Foo.new - Controller sends “new” view to browser.
  4. Create: Create controller action - HTML ‘post’ request from browser

    - Controller creates an object with attributes: - @foo = Foo.new(params) - Controller saves the new object: - @foo.save
  5. Create: full circle NEW ‘get’ the view and empty object

    Controller CREATE ‘post’ the form params to the object and save
  6. Update: Edit controller action - HTML ‘get’ request from browser

    - Controller finds the target object in DB: - @foo = Foo.find(params[:id]) - Controller sends “edit” view to browser with complete params.
  7. Update: Update controller action - HTML ‘put’ request from browser

    - Controller updates the object’s attributes: - @foo = Foo.update(params) - Controller saves the object: - @foo.save
  8. Update: full circle EDIT ‘get’ the view and object attributes

    Controller UPDATE ‘put’ the form params to the object and save
  9. CRUD REST HTML Create new get create post Read index

    get show get Update edit get update put Delete
  10. Delete: Destroy controller action - HTML ‘destroy’ request from browser

    - actually a ‘get’ request with { method: delete } options hash - Controller finds the target object in DB: - @foo = Foo.find(params[:id]) - DESTROY!!!! - @foo.destroy
  11. CRUD REST HTML Create new get create post Read index

    get show get Update edit get update put Delete destroy destroy*
  12. CRUD REST HTML Create new get create post Read index

    get show get Update edit get update put Delete destroy destroy*