KEMBAR78
Routes Controllers | KEY
ROUTES AND CONTROLLERS
ROUTES

• Abstract literal URLs from code
• The mapping between URLs and code, serves two purposes:
  • Recognize URLs and trigger a controller action
  • Generate URLs from names or objects, so you don’t have to
    hard code them in views
REST

• Representational State Transfer (REST)
• Architecture Pattern
• Supports Distributed Hypermedia
  such as the World Wide Web
• Roy Fielding, 2000
CLIENT - SERVER
STATELESS
CACHE
UNIFORM INTERFACE
REST

•   client-server
•   stateless communication
•   uniform interface between components
•   separation of concerns
    between UI and data storage
RAILS REST HELPERS
in routes.rb
resources :people



$ rake routes
people GET
 
       /people(.:format)      
 
     {:action=>"index", :controller=>"people"}
people POST
 
      /people(.:format)      
 
 {:action=>"create", :controller=>"people"}
new_person GET
     /people/new(.:format) 
{:action=>"new", :controller=>"people"}
edit_person GET
    
   /people/:id/edit(.:format)
{:action=>"edit", :controller=>"people"}
person GET
 
       /people/:id(.:format) 
 {:action=>"show", :controller=>"people"}
person PUT
 
       /people/:id(.:format) 
 {:action=>"update", :controller=>"people"}
person DELETE
      
   /people/:id(.:format) 
 {:action=>"destroy", :controller=>"people"}
DEFAULT REST
Index
The index action  provides a list of all the resources of a specific type
Sample URL: http://localhost:3000/people
HTTP GET
Defualt web page: index.html

Show
The show action displays the details of a specific resources, by default identified by its ID
Sample URL: http://localhost:3000/people/42
HTTP GET
Defualt web page: show.html
DEFAULT REST
New
The new action displays the a form for creating a new object
Sample URL: http://localhost:3000/people/new
HTTP GET
Defualt web page: new.html

Create
The create action accepts the form fields as parametrs to create a new model
Sample URL: http://localhost:3000/people
HTTP POST
no corresponding web page (re-directs to show.html to display the newly created object)
DEFAULT REST
Edit
The edit action displays the a form for modifying a specific object
Sample URL: http://localhost:3000/people/42/edit
HTTP GET
Defualt web page: edit.html

Update
The update action accepts form fields as parametrs to modify the attributes of a model
Sample URL: http://localhost:3000/people/42
HTTP PUT
no corresponding web page (re-directs to show.html to display the modified object)
DEFAULT REST
Delete
The delete action destroys a specific model
Sample URL: http://localhost:3000/people/42
HTTP DELETE
Routes Controllers

Routes Controllers

  • 1.
  • 2.
    ROUTES • Abstract literalURLs from code • The mapping between URLs and code, serves two purposes: • Recognize URLs and trigger a controller action • Generate URLs from names or objects, so you don’t have to hard code them in views
  • 3.
    REST • Representational StateTransfer (REST) • Architecture Pattern • Supports Distributed Hypermedia such as the World Wide Web • Roy Fielding, 2000
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
    REST • client-server • stateless communication • uniform interface between components • separation of concerns between UI and data storage
  • 9.
    RAILS REST HELPERS inroutes.rb resources :people $ rake routes people GET /people(.:format) {:action=>"index", :controller=>"people"} people POST /people(.:format) {:action=>"create", :controller=>"people"} new_person GET /people/new(.:format) {:action=>"new", :controller=>"people"} edit_person GET /people/:id/edit(.:format) {:action=>"edit", :controller=>"people"} person GET /people/:id(.:format) {:action=>"show", :controller=>"people"} person PUT /people/:id(.:format) {:action=>"update", :controller=>"people"} person DELETE /people/:id(.:format) {:action=>"destroy", :controller=>"people"}
  • 10.
    DEFAULT REST Index The indexaction  provides a list of all the resources of a specific type Sample URL: http://localhost:3000/people HTTP GET Defualt web page: index.html Show The show action displays the details of a specific resources, by default identified by its ID Sample URL: http://localhost:3000/people/42 HTTP GET Defualt web page: show.html
  • 11.
    DEFAULT REST New The newaction displays the a form for creating a new object Sample URL: http://localhost:3000/people/new HTTP GET Defualt web page: new.html Create The create action accepts the form fields as parametrs to create a new model Sample URL: http://localhost:3000/people HTTP POST no corresponding web page (re-directs to show.html to display the newly created object)
  • 12.
    DEFAULT REST Edit The editaction displays the a form for modifying a specific object Sample URL: http://localhost:3000/people/42/edit HTTP GET Defualt web page: edit.html Update The update action accepts form fields as parametrs to modify the attributes of a model Sample URL: http://localhost:3000/people/42 HTTP PUT no corresponding web page (re-directs to show.html to display the modified object)
  • 13.
    DEFAULT REST Delete The deleteaction destroys a specific model Sample URL: http://localhost:3000/people/42 HTTP DELETE