The 14th Round of ROR Lab.   Rails Routingfrom the Outside In         (2)          June 9th, 2012         Hyoseong Choi   ...
Non-Resourcefuldefault Rails route : match :controller(/:action(/:id))               symbols bound to parametersspecial sy...
Dynamic Segments match :controller/:action/:id/:user_id params[:id] params[:user_id]When you need namespacing controllers,...
Static Segmentsmatch :controller/:action/:id/with_user/:user_id                                                     ROR Lab.
Query String/photos/show/1?user_id=2params[:controller] = “photos”params[:action] = “show”params[:id] = 1params[:user_id] ...
Defining Defaultsmatch photos/:id => photos#showmatch photos/:id => photos#show,                                       ROR ...
Naming Routesmatch exit => sessions#destroy, :as => :logoutlogout_path, logout_url                                        ...
HTTP Verbmatch photos/show => photos#show, :via => :getget photos/showmatch photos/show => photos#show,                 :v...
Segment             Constraints    match photos/:id => photos#show,    match photos/:id => photos#show,X   match /:id => p...
Request-based                               any method on the Request object that returns aString.     match "photos",    ...
Request object attributes                            ROR Lab.
Advanced         Constraintsclass BlacklistConstraint  def initialize    @ips = Blacklist.retrieve_ips  end   def matches?...
Route Globbing: pattern matching using wildcard segmentsmatch photos/*other => photos#unknown • photos/12 • /photos/long/p...
Route Globbing             : anywhere in a routematch books/*section/:title => books#show • books/some/section/last-words-...
Route Globbing: even more than one wildcard segmentsmatch *a/foo/*b => test#index • zoo/woo/foo/bar/baz • params[:a] = “zo...
Route Globbing            : always match the optional format segment             by defaults from Rails 3.13.1~    match *...
Redirectionusing a 301 ‘moved permanently’ redirectmatch "/stories" => redirect("/posts")match "/stories/:name" => redirec...
Routing to Rack Applicationsmatch "/application.js" => Sprockets                                       ROR Lab.
Using root            “/”root :to => pages#main                           ROR Lab.
CustomizingResourceful Routesresources :photos, :controller => "images"resources :photos, :constraints => {:id => /[A-Z][A...
CustomizingResourceful Routesresources :photos, :controller => "images"                                             ROR Lab.
CustomizingResourceful Routesresources :photos, :as => "images"                                     ROR Lab.
CustomizingResourceful Routesresources :photos, :path_names => { :new => make, :edit => change }/photos/make/photos/1/chan...
CustomizingResourceful Routesscope "admin" do  resources :photos, :as => "admin_photos"end resources :photos              ...
CustomizingResourceful Routesscope ":username" do  resources :posts     /bob/posts/1                       params[:usernam...
CustomizingResourceful Routesresources :photos, :only => [:index, :show]resources :photos, :except => :destroy            ...
CustomizingResourceful Routesscope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do  resources :categories, :pa...
CustomizingResourceful Routesresources :magazines do  resources :ads, :as => periodical_adsendmagazine_periodical_ads_urle...
감사합니다.
Upcoming SlideShare
Loading in …5
×

Routing 2, Season 1

1,560
-1

Published on

The 14th Round of ROR Lab.

Published in: Education, Technology, Sports
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,560
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Routing 2, Season 1

    1. 1. The 14th Round of ROR Lab. Rails Routingfrom the Outside In (2) June 9th, 2012 Hyoseong Choi ROR Lab.
    2. 2. Non-Resourcefuldefault Rails route : match :controller(/:action(/:id)) symbols bound to parametersspecial symbols: :controller and :action ROR Lab.
    3. 3. Dynamic Segments match :controller/:action/:id/:user_id params[:id] params[:user_id]When you need namespacing controllers, match :controller(/:action(/:id)), :controller => /admin/[^/]+/ ROR Lab.
    4. 4. Static Segmentsmatch :controller/:action/:id/with_user/:user_id ROR Lab.
    5. 5. Query String/photos/show/1?user_id=2params[:controller] = “photos”params[:action] = “show”params[:id] = 1params[:user_id] = 2 ROR Lab.
    6. 6. Defining Defaultsmatch photos/:id => photos#showmatch photos/:id => photos#show, ROR Lab.
    7. 7. Naming Routesmatch exit => sessions#destroy, :as => :logoutlogout_path, logout_url ROR Lab.
    8. 8. HTTP Verbmatch photos/show => photos#show, :via => :getget photos/showmatch photos/show => photos#show, :via => [:get, :post] ROR Lab.
    9. 9. Segment Constraints match photos/:id => photos#show, match photos/:id => photos#show,X match /:id => posts#show, match /:id => posts#show, :constraints => { :id => /d.+/ } match /:username => users#show ROR Lab.
    10. 10. Request-based any method on the Request object that returns aString. match "photos", :constraints => {:subdomain => "admin"} namespace :admin do   constraints :subdomain => "admin" do     resources :photos   endhttp://api.rubyonrails.org/classes/ActionDispatch/Http/URL.html ROR Lab.
    11. 11. Request object attributes ROR Lab.
    12. 12. Advanced Constraintsclass BlacklistConstraint  def initialize    @ips = Blacklist.retrieve_ips  end   def matches?(request)    @ips.include?(request.remote_ip)  endend TwitterClone::Application.routes.draw do  match "*path" => "blacklist#index",    :constraints => BlacklistConstraint.new ROR Lab.
    13. 13. Route Globbing: pattern matching using wildcard segmentsmatch photos/*other => photos#unknown • photos/12 • /photos/long/path/to/12 • params[:other] = “12” • params[:other] = “long/path/to/12” ROR Lab.
    14. 14. Route Globbing : anywhere in a routematch books/*section/:title => books#show • books/some/section/last-words-a-memoir • params[:section] = “some/section” • params[:title] = “last-words-a-memoir” ROR Lab.
    15. 15. Route Globbing: even more than one wildcard segmentsmatch *a/foo/*b => test#index • zoo/woo/foo/bar/baz • params[:a] = “zoo/woo” • params[:b] = “bar/baz” ROR Lab.
    16. 16. Route Globbing : always match the optional format segment by defaults from Rails 3.13.1~ match *pages => pages#show3.0.x match *pages => pages#show, :format => false match *pages => pages#show, :format => true • /foo/bar.json • params[:pages] = “foo/bar” • request format => JSON ROR Lab.
    17. 17. Redirectionusing a 301 ‘moved permanently’ redirectmatch "/stories" => redirect("/posts")match "/stories/:name" => redirect("/posts/%{name}")match "/stories/:name" => redirect {|params| "/posts/#{params[:name].pluralize}" }match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }provide the leading host (http://www.example.com) ROR Lab.
    18. 18. Routing to Rack Applicationsmatch "/application.js" => Sprockets ROR Lab.
    19. 19. Using root “/”root :to => pages#main ROR Lab.
    20. 20. CustomizingResourceful Routesresources :photos, :controller => "images"resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}orconstraints(:id => /[A-Z][A-Z][0-9]+/) do  resources :photos  resources :accountsendresources :photos, :as => "images" ROR Lab.
    21. 21. CustomizingResourceful Routesresources :photos, :controller => "images" ROR Lab.
    22. 22. CustomizingResourceful Routesresources :photos, :as => "images" ROR Lab.
    23. 23. CustomizingResourceful Routesresources :photos, :path_names => { :new => make, :edit => change }/photos/make/photos/1/changescope :path_names => { :new => "make" } do  # rest of your routesend ROR Lab.
    24. 24. CustomizingResourceful Routesscope "admin" do  resources :photos, :as => "admin_photos"end resources :photos admin_photos_path, new_admin_photo_pathscope "admin", :as => "admin" do  resources :photos, :accountsend  admin_photos_path and admin_accounts_pathresources :photos, :accounts /admin/photos and /admin/accounts ROR Lab.
    25. 25. CustomizingResourceful Routesscope ":username" do  resources :posts /bob/posts/1 params[:username] ROR Lab.
    26. 26. CustomizingResourceful Routesresources :photos, :only => [:index, :show]resources :photos, :except => :destroy ROR Lab.
    27. 27. CustomizingResourceful Routesscope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do  resources :categories, :path => "kategorien"end ROR Lab.
    28. 28. CustomizingResourceful Routesresources :magazines do  resources :ads, :as => periodical_adsendmagazine_periodical_ads_urledit_magazine_periodical_ad_path ROR Lab.
    29. 29. 감사합니다.
    30. 30.   ROR Lab.
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×