16. class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @article }
end
end
end
17. Soorten Webservices
• Remote procedure calls (RPC)
• Service-oriented architecture (SOA)
• Representational state transfer (REST)
27. HTTP Abuse
Wat is er mis met dit request?
GET http://myblog.com/articles/destroy/1
28. HTTP Abuse
Wat is er mis met dit request?
GET http://myblog.com/articles/destroy/1
Conflict
29. 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
39. Resourceful URI’s
➡ 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
44. ActionController::Routing::Routes.draw do |map|
map.resources :articles do |articles|
articles.resources :comments
end
end
articles_url
article_url
new_article_url
edit_article_url
article_comments_url
article_comment_url
article_new_comment_url
article_edit_comment_url
45. ActionController::Routing::Routes.draw do |map|
map.resources :articles do |articles|
articles.resources :comments
end
end
articles_url ➡ /articles
article_url ➡ /articles/:id
new_article_url ➡ /articles/new
edit_article_url ➡ /articles/:id/edit
article_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
51. Wat zou je doen?
Je wilt comments aan articles toevoegen,
ArticlesController is aanwezig.
52. 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)
53. 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)
56. Mr. RESTful zegt:
Antwoord 2
• Een comment is een een aparte resource
57. Mr. RESTful zegt:
Antwoord 2
• Een comment is een een aparte resource
• Er bestaat geen ‘add_comment’ methode in HTTP
58. 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
59. Geen Namespaces!
• POST /articles/create
• POST /articles/create_comment
• GET /articles/destroy
• GET /articles/destroy_comment
74. 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
75. MoviesController#index
<?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
# GET /movies
<movies>
# GET /movies.xml <movie>
def index <director>Chris Miller</director>
@movies = Movie.find(:all) <id type=quot;integerquot;>1</id>
<rating type=quot;decimalquot;>7.0</rating>
respond_to do |format| <title>Shrek the Third</title>
format.html # index.html.erb </movie>
<movie>
format.xml { render :xml => @movies }
<director>Sam Raimi</director>
end
<id type=quot;integerquot;>2</id>
end
<rating type=quot;decimalquot;>6.9</rating>
<title>Spider-Man 3</title>
</movie>
<movie>
<director>Juan Carlos Fresnadillo</director>
<id type=quot;integerquot;>3</id>
<rating type=quot;decimalquot;>7.7</rating>
<title>28 Weeks Later</title>
</movie>
</movies>
77. 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
78. MoviesController#show
# GET /movies/1 <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?>
<movie>
# GET /movies/1.xml
<director>Chris Miller</director>
def show
<id type=quot;integerquot;>1</id>
@movie = Movie.find(params[:id]) <rating type=quot;decimalquot;>7.0</rating>
<title>Shrek the Third</title>
respond_to do |format| </movie>
format.html # show.html.erb
format.xml { render :xml => @movie }
end
end
96. Browser
GET /movies.html
Controller
(RESTful)
Movie.find(:all)
ActiveRecord
97. Browser
GET /movies.html
Controller
(RESTful)
Movie.find(:all)
ActiveRecord
SELECT * FROM MOVIES
DB
98. Browser Browser
GET /movies.html
Controller
(RESTful)
Movie.find(:all)
ActiveRecord
SELECT * FROM MOVIES
DB
99. Browser Browser
GET /movies.html GET /movies.html
Controller
Controller
(RESTful)
Movie.find(:all)
ActiveRecord
SELECT * FROM MOVIES
DB
100. Browser Browser
GET /movies.html GET /movies.html
Controller
Controller
(RESTful)
Movie.find(:all) Movie.find(:all)
ActiveResource ActiveRecord
SELECT * FROM MOVIES
DB
101. Browser Browser
GET /movies.html GET /movies.html
Controller
Controller
(RESTful)
Movie.find(:all) Movie.find(:all)
GET /movies.xml
ActiveResource ActiveRecord
SELECT * FROM MOVIES
DB