rails training presentation routing

Download Rails training presentation   routing

If you can't read please download the document

Upload: theacadian

Post on 21-May-2015

148 views

Category:

Business


4 download

TRANSCRIPT

  • 1. Routing

2. 2 purposes of Routing 2 purposes of Routing 1) maps Requests to Controller Action methods 2) generation of URLs To be used as arguments to methods like link_to, redirect_to 3. 1.) Mapping HTTP Requests to Controller-Actions 4. Lifecycle of a HTTP Request 5. routes.rb config/routes.rb specify 'Routing Rules' a.k.a 'Routes' hereOrder is important Rules are applied in the order in which they appearTo list all routes: rake routesTo filter routes : 6. routes.rb (contd.) NOTE Whatever URLs have to work, have to be EXPLICITLY specified in routes.rb Unless the .html file exists in public folderi.e. Even if we create Controller, ControllerAction, View if there is no route for the URL in routes.rb, then that URL will NOT work 7. routes.rb (contd.) A website may have many, many, URLs that need to work so, how to specify all of them in routes.rb ?In routes.rb, when we create 'routing rules', we can specify patterns for permitted URLs (Next few slides talk about the Syntax used for specifying those 'routing rules') 8. Legacy way that does Not work Legacy way of supplying controller and action parameters prior to Rails 3 does not work anymore # DOES NOT WORK match 'products/:id', :controller => product, :action => show 9. Regular Syntax Regular syntax use :tomatch [URL Pattern],:to => Controller#ActionE.g. match 'products/:id', :to => 'products#show' 10. ShortCut drop :to Full Syntax match 'products/:id' , :to => 'products#show'Shortcut match 'products/:id' => 'products#show'We can drop - , :to 11. Another Shortcut Syntax when path already has controller & action names Full Syntax match /projects/status, :to => projects#statusShortcut match /projects/status projects controllerstatus action 12. Constraining Request Methods and Shortcut To limit the HTTP method :via parameter match 'products/show', :to => 'products#show', :viaShortcut: get /products/show=> :get 13. Segment Keys match 'recipes/:ingredient' => recipes#index:ingredient segment key http://example.com/recipes/biryani In Controller code: we can get biryani from params[:ingredient] 14. Passing additional parameters via segment keys Possible to insert additional hardcoded parameters into route definitions match 'products/special' => 'products#show',true Accessed in Controller code via params[:special]:special => 15. Optional Segment Keys Parentheses are used to define optional segment keys match ':controller(/:action(/:id(.:format))) 16. Segment Key Constraints match 'products/:id' => 'products#show', :constraints => {:id=> /d+/}Shortcut: match 'products/:id' => 'products#show', :id => /d+/ 17. Segment Key Constraints - More Powerful constraints checking For more powerful constraints checking We can pass a 'block' to :constraints 18. Redirect Routes Possible to redirect using the redirect method match /google, :to => redirect(http://google.com/) 19. Route Globbing To grab more than 1 component Use *E.g. /items/list/base/books/fiction/dickens match 'items/list/*specs' => 'items#list'In Controller code -params[:specs] 20. 2.) Generating URLs 21. Generating URLs Original way (and simplest to understand) to generate a URL we have to supply values for the segment keys using a HashExample : link_to Products, :controller => products, :action => show, :id => @product.id 22. Generating URLs (contd.) More common way nowadays using Named Routes 23. Named Routes Created using :as parameter in a ruleExample: match item/:id => items#show, :as => item 24. Named Routes Behind the scenes What actually happens when we name a route ? 2 New methods get defined _url , _pathExample : match item/:id => items#show, :as => item above line creates 2 new methods item_path, item_urlThese methods are used with link_to to Generate Urls 25. Example Scenario: Suppose we have a Auction Site , and have Items data Goal - To display details of a particular ItemStarting Point In routes.rb, we will already have match item/:id => items#show 26. Example contd. In the View (i.e .html.erb file) link_to Auction of #{item.name}, :controller => items, :action => show, :id => item.idHow can the above code be improved by using Named Routes ? 27. Example contd. Let us make the following change in routes.rb Original Route match item/:id => items#showAppend :as parameter to create Named Route match item/:id => items#show, :as => item 28. Example contd. View code will change as follows: Original link_to statement link_to Auction of #{item.name}, :controller => items, :action => show, :id => @item.idUsing named route link_to Auction of #{item.name}, item_path (:id => @item.id) 29. Shortcuts when using Named Routes The Named route we created can further be shortened : FROMlink_to Auction of #{item.name}, item_path (:id => @item.id) TO Rails - id is default parameter, so we can drop id wherever possible link_to Auction of #{item.name}, item_path (@item.id) link_to Auction of #{item.name}, item_path (@item) 30. More complicated Named Routes Many Auctions and each Auction has Many Items URL of Item details page /auction/5/item/1Starting Point We will probably already have match auction/:auction_id/item/:id => items#show 31. More complicated Named Routes (contd.) Let us create a named route by using :as match auction/:auction_id/item/:id => items#show , :as => item 32. link_to statement changes FROM: link_to Auction of #{item.name}, :controller => items, :action => show, :auction_id => @auction.id, :id => @item.id TO link_to Auction of #{item.name}, 33. RESTful resources & Named Routes resources :auctions Above line in routes.rb creates 7 Routes out of which 4 are Named Routes 34. RESTful resources & Named Routes (contd.) resources :auctions resources :items end 35. Scoping Routing Rules 36. public folder and Routing 37. Root route & public folder In a newly generated Rails application In routes.rb root route is commented outHow is a page shown for the root URL ? http://localhost:3000/ 38. Root Route (contd.) public folder http://localhost:3000 = http://localhost:3000/index.htmlpublic folder in the root of our application root-level URLThat folder contains a index.html 39. public folder Files in this folder will be scanned before looking at of routing rules Static content is usually put hereCached content will be placed here