activeresource & rest
DESCRIPTION
Matthijs Langenberg's slide's of his presentation at the RubyenRails 2007 conf in Amsterdam.TRANSCRIPT
![Page 1: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/1.jpg)
REST & ActiveResourceMatthijs Langenberg
![Page 2: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/2.jpg)
Webservices
![Page 3: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/3.jpg)
![Page 4: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/4.jpg)
![Page 5: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/5.jpg)
![Page 6: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/6.jpg)
![Page 7: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/7.jpg)
![Page 8: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/8.jpg)
![Page 9: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/9.jpg)
Wat zijn webservices
![Page 10: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/10.jpg)
”The W3C defines a Web Service as a software system designed to support interoperable machine to machine interaction over a network.”
-- Wikipedia
![Page 11: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/11.jpg)
Bevorder ‘machine to machine interaction’
![Page 12: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/12.jpg)
HTML is moeilijk te parsen
![Page 13: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/13.jpg)
Geef iets anders terug
XML?
![Page 14: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/14.jpg)
Just Another View
![Page 15: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/15.jpg)
respond_to
![Page 16: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/16.jpg)
class ArticlesController < ApplicationController def show @article = Article.find(params[:id]) respond_to do |format| format.html format.xml { render :xml => @article } end endend
![Page 17: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/17.jpg)
Soorten Webservices
• Remote procedure calls (RPC)
• Service-oriented architecture (SOA)
• Representational state transfer (REST)
![Page 18: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/18.jpg)
Rails votes REST
![Page 19: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/19.jpg)
Rails votes REST
BIG TIME!
![Page 20: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/20.jpg)
Wat is REST?
![Page 21: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/21.jpg)
![Page 22: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/22.jpg)
![Page 23: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/23.jpg)
![Page 24: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/24.jpg)
REpresentional State Transfer
![Page 25: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/25.jpg)
HTTP’s: “convention over configuration”
![Page 26: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/26.jpg)
Schreef geen vervanging voor iets wat HTTP je gratis geeft
![Page 27: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/27.jpg)
HTTP Abuse
Wat is er mis met dit request?
GET http://myblog.com/articles/destroy/1
![Page 28: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/28.jpg)
HTTP Abuse
Wat is er mis met dit request?
GET http://myblog.com/articles/destroy/1
Conflict
![Page 29: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/29.jpg)
HTTP Abuse
Wat is er mis met dit request?
GET http://myblog.com/articles/destroy/1
Conflict
• Actie staat in URL• Uitgevoerd actie is in conflict met HTTP methode
![Page 30: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/30.jpg)
The REST-way
DELETE http://myblog.com/articles/1
![Page 31: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/31.jpg)
URI’s
![Page 32: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/32.jpg)
URI’s
GET /articles/create
GET /articles/show/1
GET /articles/update/1
GET /articles/destroy/1
![Page 33: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/33.jpg)
URI’s
GET /articles/create
GET /articles/show/1
GET /articles/update/1
GET /articles/destroy/1
![Page 34: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/34.jpg)
Mapping
![Page 35: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/35.jpg)
HTTP
GET
POST
PUT
DELETE
Mapping
![Page 36: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/36.jpg)
HTTP
GET
POST
PUT
DELETE
Controller
SHOW
CREATE
UPDATE
DESTROY
Mapping
![Page 37: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/37.jpg)
Resourceful URI’s
![Page 38: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/38.jpg)
GET /articles
POST /articles/create
GET /articles/show/1
POST /articles/update/1
GET /articles/destroy/1
Resourceful URI’s
![Page 39: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/39.jpg)
GET /articles
POST /articles/create
GET /articles/show/1
POST /articles/update/1
GET /articles/destroy/1
Resourceful URI’s
➡ GET /articles
➡ POST /articles
➡ GET /articles/1
➡ PUT /articles/1
➡ DELETE /articles/1
![Page 40: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/40.jpg)
Gratis named routes
![Page 41: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/41.jpg)
![Page 42: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/42.jpg)
ActionController::Routing::Routes.draw do |map|map.resources :articles do |articles|articles.resources :comments
endend
![Page 43: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/43.jpg)
ActionController::Routing::Routes.draw do |map|map.resources :articles do |articles|articles.resources :comments
endend
![Page 44: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/44.jpg)
articles_urlarticle_urlnew_article_urledit_article_url
article_comments_urlarticle_comment_urlarticle_new_comment_urlarticle_edit_comment_url
ActionController::Routing::Routes.draw do |map|map.resources :articles do |articles|articles.resources :comments
endend
![Page 45: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/45.jpg)
articles_urlarticle_urlnew_article_urledit_article_url
article_comments_urlarticle_comment_urlarticle_new_comment_urlarticle_edit_comment_url
➡ /articles➡ /articles/:id➡ /articles/new➡ /articles/:id/edit
➡ /articles/:article_id/comments➡ /articles/:article_id/comments/:id➡ /articles/:article_id/comments/new➡ /articles/:article_id/comments/:id/edit
ActionController::Routing::Routes.draw do |map|map.resources :articles do |articles|articles.resources :comments
endend
![Page 46: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/46.jpg)
![Page 47: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/47.jpg)
link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article }
![Page 48: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/48.jpg)
link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article }
![Page 49: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/49.jpg)
link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article }
link_to article.title, article_url(article)
![Page 50: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/50.jpg)
link_to article.title, { :controller => ‘article’, :action => ‘show’, :id => article }
link_to article.title, article_url(article)
link_to article.title, article
![Page 51: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/51.jpg)
Wat zou je doen?Je wilt comments aan articles toevoegen,ArticlesController is aanwezig.
![Page 52: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/52.jpg)
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)
![Page 53: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/53.jpg)
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)
![Page 54: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/54.jpg)
Mr. RESTful zegt:
![Page 55: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/55.jpg)
Mr. RESTful zegt:Antwoord 2
![Page 56: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/56.jpg)
Mr. RESTful zegt:Antwoord 2
• Een comment is een een aparte resource
![Page 57: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/57.jpg)
Mr. RESTful zegt:Antwoord 2
• Een comment is een een aparte resource• Er bestaat geen ‘add_comment’ methode in HTTP
![Page 58: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/58.jpg)
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
![Page 59: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/59.jpg)
Geen Namespaces!
• POST /articles/create
• POST /articles/create_comment
• GET /articles/destroy
• GET /articles/destroy_comment
![Page 60: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/60.jpg)
Teveel vrijheid is niet goed
![Page 61: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/61.jpg)
class ArticlesController < ApplicationController def show @article = Article.find(params[:id]) end
def show_rss @article = Article.find(params[:id]) render :rss => @article.to_rss end
def show_atom @article = Article.find(params[:id]) render :atom => @article.to_atom end
def show_xml @article = Article.find(params[:id]) render :xml => @article.to_xml end
def show_ajax @article = Article.find(params[:id]) render :template => show_article.rjs endend
![Page 62: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/62.jpg)
Geen aparte actie voor alternatieve view!
![Page 63: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/63.jpg)
class ArticlesController < ApplicationController def 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’ } end endend
![Page 64: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/64.jpg)
Wauw! HTTP method naar controller actie mapping actie klinkt tof!
![Page 65: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/65.jpg)
Maar er zit een adder ...
![Page 66: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/66.jpg)
Maar er zit een adder ...
![Page 67: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/67.jpg)
Browsers ondersteunen PUT en DELETE niet!
![Page 68: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/68.jpg)
Browsers ondersteunen PUT en DELETE niet!
<input name="_method" type="hidden" value="put" />
![Page 69: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/69.jpg)
Gelukkig zijn de helpers ook aangepast. ;-)
![Page 70: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/70.jpg)
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)
![Page 71: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/71.jpg)
form_forremote_form_for
Bepalen op basis van AR object de method:
form_for(Movie.new):
<form action="/movies" class="new_movie" id="new_movie" method="post">
form_for(Movie.find(:first)):
<form action="/movies/1" class="edit_movie" id="edit_movie_1" method="post">
<input name="_method" type="hidden" value="put" />
![Page 72: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/72.jpg)
Controller Acties
![Page 73: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/73.jpg)
MoviesController#index
![Page 74: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/74.jpg)
MoviesController#index
# GET /movies # GET /movies.xml def index @movies = Movie.find(:all)
respond_to do |format| format.html # index.html.erb format.xml { render :xml => @movies } end end
![Page 75: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/75.jpg)
MoviesController#index
# GET /movies # GET /movies.xml def index @movies = Movie.find(:all)
respond_to do |format| format.html # index.html.erb format.xml { render :xml => @movies } end end
<?xml version="1.0" encoding="UTF-8"?><movies> <movie> <director>Chris Miller</director> <id type="integer">1</id> <rating type="decimal">7.0</rating> <title>Shrek the Third</title> </movie> <movie> <director>Sam Raimi</director> <id type="integer">2</id> <rating type="decimal">6.9</rating> <title>Spider-Man 3</title> </movie> <movie> <director>Juan Carlos Fresnadillo</director> <id type="integer">3</id> <rating type="decimal">7.7</rating> <title>28 Weeks Later</title> </movie></movies>
![Page 76: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/76.jpg)
MoviesController#show
![Page 77: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/77.jpg)
MoviesController#show # GET /movies/1 # GET /movies/1.xml def show @movie = Movie.find(params[:id])
respond_to do |format| format.html # show.html.erb format.xml { render :xml => @movie } end end
![Page 78: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/78.jpg)
MoviesController#show # GET /movies/1 # GET /movies/1.xml def show @movie = Movie.find(params[:id])
respond_to do |format| format.html # show.html.erb format.xml { render :xml => @movie } end end
<?xml version="1.0" encoding="UTF-8"?><movie> <director>Chris Miller</director> <id type="integer">1</id> <rating type="decimal">7.0</rating> <title>Shrek the Third</title></movie>
![Page 79: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/79.jpg)
MoviesController#create
![Page 80: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/80.jpg)
MoviesController#create
# POST /movies # POST /movies.xml def create @movie = Movie.new(params[:movie])
respond_to do |format| if @movie.save flash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie,
:status => :created, :location => @movie }
else format.html { render :action => "new" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
![Page 81: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/81.jpg)
MoviesController#create
# POST /movies # POST /movies.xml def create @movie = Movie.new(params[:movie])
respond_to do |format| if @movie.save flash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie,
:status => :created, :location => @movie }
else format.html { render :action => "new" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
Status: 201 CreatedLocation: http://localhost:3000/movies/16<?xml version="1.0" encoding="UTF-8"?><movie> <director>Steven Spielbergh</director> <id type="integer">15</id> <rating type="decimal">8.3</rating> <title>Letters from Iwo Jima</title></movie>
![Page 82: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/82.jpg)
MoviesController#create
# POST /movies # POST /movies.xml def create @movie = Movie.new(params[:movie])
respond_to do |format| if @movie.save flash[:notice] = 'Movie was successfully created.' format.html { redirect_to(@movie) } format.xml { render :xml => @movie,
:status => :created, :location => @movie }
else format.html { render :action => "new" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
Status: 422 Unprocessable Entity<?xml version="1.0" encoding="UTF-8"?><errors> <error>Rating can't be blank</error> <error>Director can't be blank</error> <error>Title can't be blank</error></errors>
Status: 201 CreatedLocation: http://localhost:3000/movies/16<?xml version="1.0" encoding="UTF-8"?><movie> <director>Steven Spielbergh</director> <id type="integer">15</id> <rating type="decimal">8.3</rating> <title>Letters from Iwo Jima</title></movie>
![Page 83: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/83.jpg)
MoviesController#update
![Page 84: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/84.jpg)
MoviesController#update
# PUT /movies/1 # PUT /movies/1.xml def update @movie = Movie.find(params[:id])
respond_to do |format| if @movie.update_attributes(params[:movie]) flash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
![Page 85: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/85.jpg)
MoviesController#update
# PUT /movies/1 # PUT /movies/1.xml def update @movie = Movie.find(params[:id])
respond_to do |format| if @movie.update_attributes(params[:movie]) flash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
Status: 200 OK
![Page 86: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/86.jpg)
MoviesController#update
# PUT /movies/1 # PUT /movies/1.xml def update @movie = Movie.find(params[:id])
respond_to do |format| if @movie.update_attributes(params[:movie]) flash[:notice] = 'Movie was successfully updated.' format.html { redirect_to(@movie) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @movie.errors, :status => 422 } end end end
Status: 422 Unprocessable Entity<?xml version="1.0" encoding="UTF-8"?><errors> <error>Title can't be blank</error></errors>
Status: 200 OK
![Page 87: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/87.jpg)
MoviesController#destroy
![Page 88: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/88.jpg)
MoviesController#destroy
# DELETE /movies/1 # DELETE /movies/1.xml def destroy @movie = Movie.find(params[:id]) @movie.destroy
respond_to do |format| format.html { redirect_to(movies_url) } format.xml { head :ok } end end
![Page 89: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/89.jpg)
MoviesController#destroy
# DELETE /movies/1 # DELETE /movies/1.xml def destroy @movie = Movie.find(params[:id]) @movie.destroy
respond_to do |format| format.html { redirect_to(movies_url) } format.xml { head :ok } end end
Status: 200 OK
![Page 90: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/90.jpg)
Scaffolding
![Page 91: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/91.jpg)
maar er is meer!
![Page 92: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/92.jpg)
ActiveResource
![Page 93: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/93.jpg)
ActiveResource
• Object-oriented REST services
• Transparent met een RESTful service (Rails) werken
• Net als ActiveRecord, maar dan voor REST
![Page 94: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/94.jpg)
Browser
![Page 95: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/95.jpg)
Controller(RESTful)
Browser
GET /movies.html
![Page 96: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/96.jpg)
Controller(RESTful)
ActiveRecord
Browser
GET /movies.html
Movie.find(:all)
![Page 97: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/97.jpg)
Controller(RESTful)
ActiveRecord
Browser
DB
GET /movies.html
Movie.find(:all)
SELECT * FROM MOVIES
![Page 98: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/98.jpg)
Controller(RESTful)
Browser
ActiveRecord
Browser
DB
GET /movies.html
Movie.find(:all)
SELECT * FROM MOVIES
![Page 99: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/99.jpg)
Controller(RESTful)
Browser
ActiveRecord
Browser
Controller
DB
GET /movies.html GET /movies.html
Movie.find(:all)
SELECT * FROM MOVIES
![Page 100: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/100.jpg)
Controller(RESTful)
Browser
ActiveRecord
Browser
Controller
ActiveResource
DB
GET /movies.html
Movie.find(:all)
GET /movies.html
Movie.find(:all)
SELECT * FROM MOVIES
![Page 101: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/101.jpg)
Controller(RESTful)
Browser
ActiveRecord
Browser
Controller
ActiveResource
DB
GET /movies.html
Movie.find(:all)
GET /movies.xml
GET /movies.html
Movie.find(:all)
SELECT * FROM MOVIES
![Page 102: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/102.jpg)
Configuratie
![Page 103: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/103.jpg)
Werken met RESTful webservices(ActiveResource)
![Page 104: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/104.jpg)
Create
![Page 105: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/105.jpg)
Create
![Page 106: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/106.jpg)
Read
![Page 107: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/107.jpg)
Read
![Page 108: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/108.jpg)
Update
![Page 109: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/109.jpg)
Update
![Page 110: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/110.jpg)
Destroy
![Page 111: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/111.jpg)
Destroy
![Page 112: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/112.jpg)
Wat doet ARes?
![Page 113: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/113.jpg)
Wat doet ARes?
• Genereer URL
![Page 114: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/114.jpg)
Wat doet ARes?
• Genereer URL
• Request URL (XML)
![Page 115: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/115.jpg)
Wat doet ARes?
• Genereer URL
• Request URL (XML)
• Verwerk request (XML)
![Page 116: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/116.jpg)
Wat doet ARes?
• Genereer URL
• Request URL (XML)
• Verwerk request (XML)
• Biedt ActiveRecord-like API
![Page 117: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/117.jpg)
Demo
![Page 118: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/118.jpg)
Demo
1) RESTful Rails applicatie (met curl)
![Page 119: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/119.jpg)
Demo
1) RESTful Rails applicatie (met curl)
2) Rails applicatie met ActiveResource
![Page 120: ActiveResource & REST](https://reader037.vdocument.in/reader037/viewer/2022103016/5552bf28b4c90581158b4645/html5/thumbnails/120.jpg)
Demo
1) RESTful Rails applicatie (met curl)
2) Rails applicatie met ActiveResource
3) ActiveResource gem