Como programar un blog REST

2,792 views
2,680 views

Published on

Como programar un blog REST

Published in: Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,792
On SlideShare
0
From Embeds
0
Number of Embeds
71
Actions
Shares
0
Downloads
29
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Como programar un blog REST

  1. 1. Cómo programar un blog REST Javier Vidal Postigo http://javiervidal.net/ Conferencia Rails 2007
  2. 2. ¿Qué es Rails?
  3. 3. framework ruby para desarrollar aplicaciones web con acceso a base de datos
  4. 4. framework ruby para desarrollar aplicaciones web con acceso a base de datos convención mejor que configuración
  5. 5. ¿Qué es MVC?
  6. 6. MVC en Rails DB Enrutador Vista Modelo Controlador
  7. 7. MVC en Rails DB Enrutador Vista Modelo Controlador
  8. 8. MVC en Rails DB Enrutador Vista Modelo Controlador
  9. 9. MVC en Rails DB Enrutador Vista Modelo Controlador
  10. 10. MVC en Rails DB Enrutador Vista Modelo Controlador
  11. 11. MVC en Rails DB Enrutador Vista Modelo Controlador
  12. 12. MVC en Rails DB Enrutador Vista Modelo Controlador
  13. 13. <ul><li>http://server/controller/action/id </li></ul><ul><li>http://server/posts/show/1 </li></ul>
  14. 14. ¿Qué es REST?
  15. 15. REST => “Representational State Transfer” <ul><li>modelo de arquitectura de aplicaciones </li></ul><ul><li>las aplicaciones gestionan recursos </li></ul><ul><li>las operaciones son los verbos HTTP </li></ul>
  16. 16. REST => “Representational State Transfer” <ul><li>modelo de arquitectura de aplicaciones </li></ul><ul><li>las aplicaciones gestionan recursos </li></ul><ul><li>las operaciones son los verbos HTTP </li></ul>recurso URI instancia de modelo
  17. 17. REST => “Representational State Transfer” <ul><li>modelo de arquitectura de aplicaciones </li></ul><ul><li>las aplicaciones gestionan recursos </li></ul><ul><li>las operaciones son los verbos HTTP </li></ul>recurso URI instancia de modelo operación verbo HTTP acción del controlador
  18. 18. Create Read Update Delete
  19. 19. Create Read Update Delete insert find update delete
  20. 20. Create Read Update Delete insert find update delete POST GET UPDATE DELETE
  21. 21. Create Read Update Delete insert find update delete POST GET UPDATE DELETE create show update destroy
  22. 22. <ul><li>POST </li></ul><ul><li>GET </li></ul><ul><li>PUT </li></ul><ul><li>DELETE </li></ul><ul><li>/posts </li></ul><ul><li>/posts/1 </li></ul><ul><li>/posts/1 </li></ul><ul><li>/posts/1 </li></ul>
  23. 23. <ul><li># GET /posts/1 </li></ul><ul><li>def show ... end </li></ul><ul><li># POST /posts </li></ul><ul><li>def create ... end </li></ul><ul><li># PUT /posts/1 </li></ul><ul><li>def update ... end </li></ul><ul><li># DELETE /posts/1 </li></ul><ul><li>def destroy ... end </li></ul><ul><li>end </li></ul>class PostsController < ApplicationController
  24. 24. <ul><li># GET /posts/1 </li></ul><ul><li>def show ... end </li></ul><ul><li># POST /posts </li></ul><ul><li>def create ... end </li></ul><ul><li># PUT /posts/1 </li></ul><ul><li>def update ... end </li></ul><ul><li># DELETE /posts/1 </li></ul><ul><li>def destroy ... end </li></ul><ul><li>end </li></ul><ul><li>link_to &quot;#{post.title}&quot; , post_url(post) </li></ul>class PostsController < ApplicationController
  25. 25. <ul><li># GET /posts/1 </li></ul><ul><li>def show ... end </li></ul><ul><li># POST /posts </li></ul><ul><li>def create ... end </li></ul><ul><li># PUT /posts/1 </li></ul><ul><li>def update ... end </li></ul><ul><li># DELETE /posts/1 </li></ul><ul><li>def destroy ... end </li></ul><ul><li>end </li></ul><ul><li>form_for( :post , </li></ul><ul><li>:url => posts_path) do |f| </li></ul><ul><li>f.text_field :title </li></ul><ul><li>f.text_area :body </li></ul><ul><li>submit_tag &quot;Create&quot; </li></ul><ul><li>end </li></ul>class PostsController < ApplicationController
  26. 26. <ul><li># GET /posts/1 </li></ul><ul><li>def show ... end </li></ul><ul><li># POST /posts </li></ul><ul><li>def create ... end </li></ul><ul><li># PUT /posts/1 </li></ul><ul><li>def update ... end </li></ul><ul><li># DELETE /posts/1 </li></ul><ul><li>def destroy ... end </li></ul><ul><li>end </li></ul><ul><li>form_for( :post , </li></ul><ul><li>:url => post_path( @post ), </li></ul><ul><li>:html => {:method => :put }) do |f| </li></ul><ul><li>f.text_field :title </li></ul><ul><li>f.text_area :body </li></ul><ul><li>submit_tag &quot;Update&quot; </li></ul><ul><li>end </li></ul>class PostsController < ApplicationController
  27. 27. <ul><li># GET /posts/1 </li></ul><ul><li>def show ... end </li></ul><ul><li># POST /posts </li></ul><ul><li>def create ... end </li></ul><ul><li># PUT /posts/1 </li></ul><ul><li>def update ... end </li></ul><ul><li># DELETE /posts/1 </li></ul><ul><li>def destroy ... end </li></ul><ul><li>end </li></ul><ul><li>link_to 'Borrar' , post_path(post), </li></ul><ul><li>:method => :delete </li></ul>class PostsController < ApplicationController
  28. 28. Todavía hay más
  29. 29. class PostsController < ApplicationController # GET /posts def index ... end # GET /posts/1 def show ... end # GET /posts/new def new ... end # GET /posts/1/edit def edit ... end # POST /posts def create ... end # PUT /posts/1 def update ... end # DELETE /posts/1 def destroy ... end end
  30. 30. class PostsController < ApplicationController # GET /posts def index ... end # GET /posts/1 def show ... end # GET /posts/new def new ... end # GET /posts/1/edit def edit ... end # POST /posts def create ... end # PUT /posts/1 def update ... end # DELETE /posts/1 def destroy ... end end
  31. 31. class PostsController < ApplicationController # GET /posts def index ... end # GET /posts/1 def show ... end # GET /posts/new def new ... end # GET /posts/1/edit def edit ... end # POST /posts def create ... end # PUT /posts/1 def update ... end # DELETE /posts/1 def destroy ... end end
  32. 32. class PostsController < ApplicationController # GET /posts def index ... end # GET /posts/1 def show ... end # GET /posts/new def new ... end # GET /posts/1/edit def edit ... end # POST /posts def create ... end # PUT /posts/1 def update ... end # DELETE /posts/1 def destroy ... end end
  33. 33. <ul><li>GET </li></ul><ul><li>GET </li></ul><ul><li>GET </li></ul><ul><li>GET </li></ul><ul><li>POST </li></ul><ul><li>PUT </li></ul><ul><li>DELETE </li></ul><ul><li>/posts </li></ul><ul><li>/posts/1 </li></ul><ul><li>/posts/new </li></ul><ul><li>/posts/1/edit </li></ul><ul><li>/posts </li></ul><ul><li>/posts/1 </li></ul><ul><li>/posts/1 </li></ul><ul><li>index </li></ul><ul><li>show </li></ul><ul><li>new </li></ul><ul><li>edit </li></ul><ul><li>create </li></ul><ul><li>update </li></ul><ul><li>destroy </li></ul><ul><li>posts_path </li></ul><ul><li>post_path(1) </li></ul><ul><li>new_post_path </li></ul><ul><li>edit_post_path(1) </li></ul><ul><li>posts_path </li></ul><ul><li>post_path(1) </li></ul><ul><li>post_path(1) </li></ul>
  34. 34. <ul><li>class PostsController < ApplicationController </li></ul><ul><li>def add_comment ... end </li></ul><ul><li>end </li></ul>
  35. 35. <ul><li>class PostsController < ApplicationController </li></ul><ul><li>def add_comment ... end </li></ul><ul><li>end </li></ul><ul><li>class CommentsController < ApplicationController </li></ul><ul><li>def assign_to_post ... end </li></ul><ul><li>end </li></ul>
  36. 36. <ul><li>class PostsController < ApplicationController </li></ul><ul><li>def add_comment ... end </li></ul><ul><li>end </li></ul><ul><li>class CommentsController < ApplicationController </li></ul><ul><li>def assign_to_post ... end </li></ul><ul><li>end </li></ul><ul><ul><li>recursos anidados </li></ul></ul>
  37. 37. <ul><li>class PostsController < ApplicationController </li></ul><ul><li>def add_comment ... end </li></ul><ul><li>end </li></ul><ul><li>class CommentsController < ApplicationController </li></ul><ul><li>def assign_to_post ... end </li></ul><ul><li>end </li></ul><ul><ul><li>recursos anidados </li></ul></ul><ul><ul><li>añadiendo un nuevo recurso “membership” </li></ul></ul>
  38. 38. <ul><li>class Post < ActiveRecord::Base </li></ul><ul><li>has_many :memberships </li></ul><ul><li>has_many :comments , :through => :memberships </li></ul><ul><li>end </li></ul><ul><li>class Comment < ActiveRecord::Base </li></ul><ul><li>has_many :memberships </li></ul><ul><li>end </li></ul><ul><li>class Membership < ActiveRecord::Base </li></ul><ul><li>belongs_to :comment </li></ul><ul><li>belongs_to :post </li></ul><ul><li>end </li></ul>post membership comment
  39. 39. RailsConf '06: World of Resources David Heinemeier Hansson http://media.rubyonrails.org/presentations/worldofresources.pdf Desarrollo REST con Rails Ralf Wirdemann y Thomas Baustert (traducción de Juán Lupión) http://www.b-simple.de/download/restful_rails_es.pdf RESTful Web Services Leonard Richardson y Sam Ruby http://www.amazon.com/ Referencias
  40. 40. http://javiervidal.net/ [email_address]

×