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

5a28832fb492a80356e39677359fc079?s=128

Viking Education

July 17, 2015
Tweet

More Decks by Viking Education

Other Decks in Technology

Transcript

  1. Putting CRUD to REST

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

    REST. - Give you a mental model for relating RESTful controller actions to CRUD principles.
  3. Quick CRUD review - Create - Read - Update -

    Delete
  4. 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.
  5. CRUD REST HTML Create Read index get show get Update

    Delete
  6. Create: New controller action - HTML ‘get’ request from browser

    - Controller creates an empty object: - @foo = Foo.new - Controller sends “new” view to browser.
  7. 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
  8. Create: full circle NEW ‘get’ the view and empty object

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

    get show get Update Delete
  10. 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.
  11. 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
  12. Update: full circle EDIT ‘get’ the view and object attributes

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

    get show get Update edit get update put Delete
  14. 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
  15. CRUD REST HTML Create new get create post Read index

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

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