Routes

1,300 views

Published on

IN this presentation, I have tried to explain the routing system for the Ruby on Rails web applications

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,300
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Routes

  1. 1. the easiest way to develop web applications ruby on rails Routes Presented By : Dharin Rajgor
  2. 2. routes <ul><li>Let’s see routing system! </li></ul>
  3. 4. function of routing system <ul><ul><li>interpreting a request URL </li></ul></ul><ul><ul><ul><li>which controller to instantiate </li></ul></ul></ul><ul><ul><ul><li>which action to invoke for that controller </li></ul></ul></ul><ul><ul><ul><li>pass parameters, if any </li></ul></ul></ul><ul><ul><li>generating a request URL </li></ul></ul><ul><ul><ul><li>instantiate the specific controller </li></ul></ul></ul><ul><ul><ul><li>call the appropriate action </li></ul></ul></ul><ul><ul><ul><li>pass parameters, if any </li></ul></ul></ul>
  4. 5. default page <ul><ul><li>what is default page? </li></ul></ul><ul><ul><li>convention over configuration (coc) </li></ul></ul>
  5. 6. default page (cntd…) <ul><ul><li>what to say to rails for going to the page? </li></ul></ul><ul><ul><ul><li>when you get http://localhost:3000 </li></ul></ul></ul><ul><ul><ul><ul><li>instantiate controller “products_controller” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>invoke action “index” </li></ul></ul></ul></ul><ul><ul><ul><li>empty URL ‘’ and so when you get ‘’ </li></ul></ul></ul><ul><ul><ul><ul><li>instantiate controller “products_controller” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>invoke action “index” ….. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>instantiate controller “products” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>invoke action “index” ….. </li></ul></ul></ul></ul><ul><ul><ul><ul><li>:controller => “products” </li></ul></ul></ul></ul><ul><ul><ul><ul><li>:action => “index” </li></ul></ul></ul></ul>
  6. 7. in config/route.rb :map.connect '', :controller => “products&quot;, :action => &quot;index“ * Read it like this: When an empty URL is received , the index action will be called on the products controller
  7. 8. check the path and its generator product_catalog > ruby script/console >> rts = ActionController::Routing::Routes >> rts.recognize_path(“”) => {:action=>”index”, :controller=>”products”} >> rts.generate(:controller=>“products&quot;,:action=>&quot;index&quot;) => ””
  8. 9. route ordering :map.connect '', :controller => “products&quot;, :action => &quot;index“ :map.connect ‘home', :controller => “products&quot;, :action => &quot;index“ * Read it like this: When http://localhost:3000/home is received, the index action will be called on the albums controller
  9. 10. show, delete, edit, add :map.connect ‘home', :controller => “products&quot;, :action => &quot;index“ :map.connect '', :controller => “products&quot;, :action => &quot;index“ :map.connect ‘home/show/:id', :controller => “products&quot;, :action => “show“ :map.connect ‘home/delete/:id', :controller => “products&quot;, :action => “delete“ :map.connect ‘home/edit/:id', :controller => “products&quot;, :action => “edit“ :map.connect ‘home/update/:id', :controller => “products&quot;, :action => “update“ :map.connect ‘home/new', :controller => “products&quot;, :action => “new“ :map.connect ‘home/create', :controller => “products&quot;, :action => “create“
  10. 11. default route <ul><li>how routing rules matched? </li></ul><ul><li>Receptor </li></ul><ul><ul><li>:id </li></ul></ul><ul><li>Magic receptor </li></ul><ul><ul><li>:action </li></ul></ul><ul><li>:map.connect ‘home', :controller => “products&quot;, :action => &quot;index“ </li></ul><ul><li>:map.connect ‘home/:action/:id', :controller => “products&quot; </li></ul><ul><li>:map.connect ‘home/new', :controller => “products&quot;, :action => “new“ </li></ul><ul><li>:map.connect ‘home/create', :controller => “products&quot;, :action => “create“ </li></ul><ul><ul><li>even more shorter… </li></ul></ul><ul><li>:map.connect ‘home', :controller => “products&quot;, :action => &quot;index“ </li></ul><ul><li>:map.connect ‘home/:action/:id', :controller => “products&quot; </li></ul><ul><li>:map.connect ‘home/:action', :controller => “products&quot; </li></ul>
  11. 12. default route (cntd…) <ul><li>Even shorter… </li></ul><ul><ul><li>  map.connect ‘products/:action/:id', :controller => “products“ </li></ul></ul><ul><li>what about other controller? </li></ul><ul><li>another receptor </li></ul><ul><ul><li>: controller </li></ul></ul><ul><ul><ul><li>:map.connect ‘:controller/:action/:id‘ - default route </li></ul></ul></ul>
  12. 13. named routes <ul><ul><li>why do we need? </li></ul></ul><ul><ul><li>how could we do? </li></ul></ul><ul><ul><ul><li>:map.connect ‘home', :controller => “products&quot;, :action => &quot;index“ </li></ul></ul></ul><ul><ul><ul><li>to </li></ul></ul></ul><ul><ul><ul><li>:map.home ‘home', :controller => “products&quot;, :action => &quot;index“ </li></ul></ul></ul><ul><ul><li>dynamically generate two methods </li></ul></ul><ul><ul><ul><li>name_url </li></ul></ul></ul><ul><ul><ul><li>name_path </li></ul></ul></ul>
  13. 14. routes.rb ActionController::Routing::Routes.draw do |map| map.resources :products map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format‘ End
  14. 15. to get going … <ul><li>in order to have a fully working development environment, you will need to install: </li></ul><ul><li>Ruby - the language </li></ul><ul><li>Ruby Gems - the plug-in manager for Ruby </li></ul><ul><li>SciTE - Editor for Ruby </li></ul><ul><li>MySQL Server - the database server </li></ul><ul><li>MySQL query Browser / MySQL Administrator - GUI to create databases, add users and create tables </li></ul><ul><li>Rails Framework and other gems </li></ul>
  15. 16. thank you Questions?

×