ActiveResource & REST

  • View
    2.751

  • Download
    1

Embed Size (px)

DESCRIPTION

Matthijs Langenberg's slide's of his presentation at the RubyenRails 2007 conf in Amsterdam.

Text of ActiveResource & REST

  • 1.REST & ActiveResource Matthijs Langenberg

2. Webservices 3. Wat zijn webservices 4. The W3C denes a Web Service as a software system designed to supportinteroperable machine to machine interaction over a network.-- Wikipedia 5. Bevorder machine to machine interaction 6. HTML is moeilijk te parsen 7. Geef iets anders terugXML? 8. Just Another View 9. respond_to 10. class ArticlesController < ApplicationControllerdef show @article = Article.find(params[:id]) respond_to do |format| format.html format.xml { render :xml => @article } endend end 11. Soorten Webservices Remote procedure calls (RPC) Service-oriented architecture (SOA) Representational state transfer (REST) 12. Rails votes REST 13. Rails votes REST BIG TIME! 14. Wat is REST? 15. REpresentional State Transfer 16. HTTPs: convention over conguration 17. Schreef geen vervanging voor iets wat HTTP je gratis geeft 18. HTTP Abuse Wat is er mis met dit request? GET http://myblog.com/articles/destroy/1 19. HTTP Abuse Wat is er mis met dit request? GET http://myblog.com/articles/destroy/1 Conict 20. HTTP Abuse Wat is er mis met dit request? GET http://myblog.com/articles/destroy/1 Conict Actie staat in URL Uitgevoerd actie is in conict met HTTP methode 21. The REST-way DELETE http://myblog.com/articles/1 22. URIs 23. URIsGET /articles/createGET /articles/show/1GET /articles/update/1GET /articles/destroy/1 24. URIsGET /articles/createGET /articles/show/1GET /articles/update/1GET /articles/destroy/1 25. Mapping 26. MappingHTTP GETPOST PUTDELETE 27. MappingHTTP Controller GETSHOWPOSTCREATE PUTUPDATEDELETE DESTROY 28. Resourceful URIs 29. Resourceful URIsGET /articlesPOST /articles/createGET /articles/show/1POST /articles/update/1GET /articles/destroy/1 30. Resourceful URIs GET /articles GET /articles POST /articles POST /articles/create GET /articles/1 GET /articles/show/1 PUT /articles/1 POST /articles/update/1 DELETE /articles/1 GET /articles/destroy/1 31. Gratis named routes 32. ActionController::Routing::Routes.draw do |map| map.resources :articles do |articles| articles.resources :comments end end 33. ActionController::Routing::Routes.draw do |map| map.resources :articles do |articles| articles.resources :comments end end 34. ActionController::Routing::Routes.draw do |map|map.resources :articles do |articles|articles.resources :commentsendendarticles_url article_url new_article_url edit_article_urlarticle_comments_url article_comment_url article_new_comment_url article_edit_comment_url 35. ActionController::Routing::Routes.draw do |map|map.resources :articles do |articles|articles.resources :commentsendendarticles_url /articles article_url /articles/:id new_article_url /articles/new edit_article_url /articles/:id/editarticle_comments_url /articles/:article_id/comments article_comment_url /articles/:article_id/comments/:id article_new_comment_url /articles/:article_id/comments/new article_edit_comment_url /articles/:article_id/comments/:id/edit 36. link_to article.title, {:controller => article, :action => show, :id => article } 37. link_to article.title, {:controller => article, :action => show, :id => article } 38. link_to article.title,{:controller => article,:action => show,:id => article } link_to article.title, article_url(article) 39. link_to article.title,{:controller => article,:action => show,:id => article } link_to article.title, article_url(article) link_to article.title, article 40. Wat zou je doen? Je wilt comments aan articles toevoegen, ArticlesController is aanwezig. 41. Wat zou je doen? Je wilt comments aan articles toevoegen, ArticlesController is aanwezig. 1) Voeg een actie add_comment aan ArticlesController toe. (POST /articles/1/add_comment) 42. Wat zou je doen? Je wilt comments aan articles toevoegen, ArticlesController is aanwezig. 1) Voeg een actie add_comment aan ArticlesController toe. (POST /articles/1/add_comment)2) Maak een CommentsController, met een create actie. (POST /comments/create?article_id=1) 43. Mr. RESTful zegt: 44. Mr. RESTful zegt:Antwoord 2 45. Mr. RESTful zegt: Antwoord 2 Een comment is een een aparte resource 46. Mr. RESTful zegt:Antwoord 2 Een comment is een een aparte resource Er bestaat geen add_comment methode in HTTP 47. Mr. RESTful zegt: Antwoord 2 Een comment is een een aparte resource Er bestaat geen add_comment methode in HTTP Er bestaat wel een create (POST) methode in HTTP 48. Geen Namespaces! POST /articles/create POST /articles/create_comment GET /articles/destroy GET /articles/destroy_comment 49. Teveel vrijheid is niet goed 50. class ArticlesController < ApplicationControllerdef [email protected] = Article.find(params[:id])end def [email protected] = Article.find(params[:id])render :rss => @article.to_rssend def [email protected] = Article.find(params[:id])render :atom => @article.to_atomend def [email protected] = Article.find(params[:id])render :xml => @article.to_xmlend def show_ajax @article = Article.find(params[:id]) render :template => show_article.rjsend end 51. Geen aparte actie voor alternatieve view! 52. class ArticlesController < ApplicationControllerdef show @article = Article.find(params[:id]) respond_to do |format| format.html format.rss { render :rss => @article.to_rss } format.atom { render :atom => @article.to_atom } format.xml { render :xml => @article.to_xml } format.rjs { render :template => show_article.rjs } endend end 53. Wauw! HTTP method naar controller actie mapping actie klinkt tof! 54. Maar er zit een adder ... 55. Maar er zit een adder ... 56. Browsers ondersteunen PUT en DELETE niet! 57. Browsers ondersteunen PUT en DELETE niet! 58. Gelukkig zijn de helpers ookaangepast. ;-) 59. HTML_options link_to delete, article_path(1), :method => delete link_to_remote, delete, article_path(1), :method => delete form_tag(member_path(2), :method => :put) 60. form_for remote_form_for Bepalen op basis van AR object de method: form_for(Movie.new): form_for(Movie.find(:first)): 61. Controller Acties 62. MoviesController#index 63. MoviesController#index # GET /movies # GET /movies.xml def [email protected] = Movie.nd(:all) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @movies }end end 64. MoviesController#index # GET /movies # GET /movies.xml def index Chris [email protected] = Movie.nd(:all)17.0respond_to do |format| Shrek the Third format.html # index.html.erb format.xml { render :xml => @movies } Sam Raimiend 2 end 6.9Spider-Man 3Juan Carlos Fresnadillo37.728 Weeks Later 65. MoviesController#show 66. MoviesController#show # GET /movies/1 # GET /movies/1.xml def [email protected] = Movie.nd(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @movie }end end 67. MoviesController#show # GET /movies/1 # GET /movies/1.xml Chris Miller def show [email protected] = Movie.nd(params[:id])7.0Shrek the Thirdrespond_to do |format| format.html # show.html.erb format.xml { render :xml => @movie }end end 68. MoviesController#create 69. MoviesController#create # POST /movies # POST /movies.xml def [email protected] = Movie.new(params[:movie]) respond_to do |format| if @movie.save ash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie, :status => :created, :location => @movie } else format.html { render :action => quot;newquot; } format.xml { render :xml => @movie.errors, :status => 422 } endend end 70. MoviesController#create Status: 201 Created Location: http://localhost:3000/movies/16 # POST /movies # POST /movies.xml def createSteven [email protected] = Movie.new(params[:movie])158.3respond_to do |format|Letters from Iwo Jima if @movie.save ash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie, :status => :created, :location => @movie } else format.html { render :action => quot;newquot; } format.xml { render :xml => @movie.errors, :status => 422 } endend end 71. MoviesController#create Status: 201 Created Location: http://localhost:3000/movies/16 # POST /movies # POST /movies.xml def createSteven [email protected] = Movie.new(params[:movie])158.3respond_to do |format|Letters from Iwo Jima if @movie.save ash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie, :status => :created, Status: 422 Unprocessable Entity :location => @movie } else format.html { render :action => quot;newquot; }Rating can't be blank format.xml { render :xml => @movie.errors, :status => 422 }Director can't be blank endTitle can't be blankend end 72. MoviesController#update 73. MoviesController#update # PUT /movies/1 # PUT /movies/1.xml def [email protected] = Movie.nd(params[:id]) respond_to do |format| if @movie.update_attributes(params[:movie]) ash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => quot;editquot; } format.xml { render :xml => @movie.errors, :status => 422 } endend end 74. MoviesController#update Status: 200 OK # PUT /movies/1 # PUT /movies/1.xml def [email protected] = Movie.nd(params[:id]) respond_to do |format| if @movie.update_attributes(params[:movie]) ash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => quot;editquot; } format.xml { render :xml => @movie.errors, :status => 422 } endend end 75. MoviesController#update Status: 200 OK # PUT /movies/1 # PUT /movies/1.xml def [email protected] = Movie.nd(params[:id]) Status: 422 Unprocessable Entityrespond_to do |format| if @movie.update_attributes(params[:movie]) ash[:notice] = 'Movie was successfully updated.'Title can't be blank format.html { redirect_to(@movie) } format.x

Search related