The 14th Round of ROR Lab.


   Rails Routing
from the Outside In
         (2)
          June 9th, 2012

         Hyoseong Choi
           ROR Lab.
Non-Resourceful
default Rails route :

 match ':controller(/:action(/:id))'

               symbols bound to parameters
special symbols:
   :controller and :action

                                       ROR Lab.
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.
Static Segments

match ':controller/:action/:id/with_user/:user_id'




                                                     ROR Lab.
Query String

/photos/show/1?user_id=2




params[:controller] = “photos”
params[:action] = “show”
params[:id] = 1
params[:user_id] = 2


                                 ROR Lab.
Defining Defaults
match 'photos/:id' => 'photos#show'




match 'photos/:id' => 'photos#show',




                                       ROR Lab.
Naming Routes

match 'exit' => 'sessions#destroy', :as => :logout




logout_path, logout_url




                                                     ROR Lab.
HTTP Verb
match 'photos/show' => 'photos#show', :via => :get




get 'photos/show'




match 'photos/show' => 'photos#show',
                 :via => [:get, :post]




                                                     ROR Lab.
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.
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
       end




http://api.rubyonrails.org/classes/ActionDispatch/Http/URL.html


                                                                                ROR Lab.
Request object attributes




                            ROR Lab.
Advanced
         Constraints
class BlacklistConstraint
  def initialize
    @ips = Blacklist.retrieve_ips
  end
 
  def matches?(request)
    @ips.include?(request.remote_ip)
  end
end
 
TwitterClone::Application.routes.draw do
  match "*path" => "blacklist#index",
    :constraints => BlacklistConstraint.new




                                              ROR Lab.
Route Globbing
: pattern matching using wildcard segments

match 'photos/*other' => 'photos#unknown'


 • photos/12
 • /photos/long/path/to/12
 • params[:other] = “12”
 • params[:other] = “long/path/to/12”
                                            ROR Lab.
Route Globbing
             : anywhere in a route

match 'books/*section/:title' => 'books#show'




 • books/some/section/last-words-a-memoir

 • params[:section] = “some/section”
 • params[:title] = “last-words-a-memoir”
                                                ROR Lab.
Route Globbing
: even more than one wildcard segments

match '*a/foo/*b' => 'test#index'



 • zoo/woo/foo/bar/baz

 • params[:a] = “zoo/woo”
 • params[:b] = “bar/baz”
                                    ROR Lab.
Route Globbing
            : always match the optional format segment
             by defaults from Rails 3.1


3.1~    match '*pages' => 'pages#show'



3.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.
Redirection
using a 301 ‘moved permanently’ redirect

match "/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.
Routing to Rack
 Applications

match "/application.js" => Sprockets




                                       ROR Lab.
Using root
            “/”

root :to => 'pages#main'




                           ROR Lab.
Customizing
Resourceful Routes
resources :photos, :controller => "images"




resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}
or
constraints(:id => /[A-Z][A-Z][0-9]+/) do
  resources :photos
  resources :accounts
end




resources :photos, :as => "images"




                                                                 ROR Lab.
Customizing
Resourceful Routes
resources :photos, :controller => "images"




                                             ROR Lab.
Customizing
Resourceful Routes
resources :photos, :as => "images"




                                     ROR Lab.
Customizing
Resourceful Routes
resources :photos, :path_names => { :new => 'make', :edit => 'change' }




/photos/make
/photos/1/change




scope :path_names => { :new => "make" } do
  # rest of your routes
end




                                                                          ROR Lab.
Customizing
Resourceful Routes
scope "admin" do
  resources :photos, :as => "admin_photos"
end
 
resources :photos
                                             admin_photos_path, new_admin_photo_path




scope "admin", :as => "admin" do
  resources :photos, :accounts
end
                                             admin_photos_path and admin_accounts_path
resources :photos, :accounts
                                                 /admin/photos and /admin/accounts




                                                                                ROR Lab.
Customizing
Resourceful Routes

scope ":username" do
  resources :posts     /bob/posts/1
                       params[:username]




                                           ROR Lab.
Customizing
Resourceful Routes

resources :photos, :only => [:index, :show]
resources :photos, :except => :destroy




                                              ROR Lab.
Customizing
Resourceful Routes
scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
  resources :categories, :path => "kategorien"
end




                                                                    ROR Lab.
Customizing
Resourceful Routes
resources :magazines do
  resources :ads, :as => 'periodical_ads'
end




magazine_periodical_ads_url

edit_magazine_periodical_ad_path




                                            ROR Lab.
감사합니다.

Routing 2, Season 1

  • 1.
    The 14th Roundof ROR Lab. Rails Routing from the Outside In (2) June 9th, 2012 Hyoseong Choi ROR Lab.
  • 2.
    Non-Resourceful default Rails route: match ':controller(/:action(/:id))' symbols bound to parameters special symbols: :controller and :action ROR Lab.
  • 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.
  • 5.
    Query String /photos/show/1?user_id=2 params[:controller] =“photos” params[:action] = “show” params[:id] = 1 params[:user_id] = 2 ROR Lab.
  • 6.
    Defining Defaults match 'photos/:id'=> 'photos#show' match 'photos/:id' => 'photos#show', ROR Lab.
  • 7.
    Naming Routes match 'exit'=> 'sessions#destroy', :as => :logout logout_path, logout_url ROR Lab.
  • 8.
    HTTP Verb match 'photos/show'=> 'photos#show', :via => :get get 'photos/show' match 'photos/show' => 'photos#show', :via => [:get, :post] ROR Lab.
  • 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.
    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   end http://api.rubyonrails.org/classes/ActionDispatch/Http/URL.html ROR Lab.
  • 11.
  • 12.
    Advanced Constraints class BlacklistConstraint   def initialize     @ips = Blacklist.retrieve_ips   end     def matches?(request)     @ips.include?(request.remote_ip)   end end   TwitterClone::Application.routes.draw do   match "*path" => "blacklist#index",     :constraints => BlacklistConstraint.new ROR Lab.
  • 13.
    Route Globbing : patternmatching using wildcard segments match 'photos/*other' => 'photos#unknown' • photos/12 • /photos/long/path/to/12 • params[:other] = “12” • params[:other] = “long/path/to/12” ROR Lab.
  • 14.
    Route Globbing : anywhere in a route match '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.
    Route Globbing : evenmore than one wildcard segments match '*a/foo/*b' => 'test#index' • zoo/woo/foo/bar/baz • params[:a] = “zoo/woo” • params[:b] = “bar/baz” ROR Lab.
  • 16.
    Route Globbing : always match the optional format segment by defaults from Rails 3.1 3.1~ match '*pages' => 'pages#show' 3.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.
    Redirection using a 301‘moved permanently’ redirect match "/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.
    Routing to Rack Applications match "/application.js" => Sprockets ROR Lab.
  • 19.
    Using root “/” root :to => 'pages#main' ROR Lab.
  • 20.
    Customizing Resourceful Routes resources :photos,:controller => "images" resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/} or constraints(:id => /[A-Z][A-Z][0-9]+/) do   resources :photos   resources :accounts end resources :photos, :as => "images" ROR Lab.
  • 21.
    Customizing Resourceful Routes resources :photos,:controller => "images" ROR Lab.
  • 22.
  • 23.
    Customizing Resourceful Routes resources :photos,:path_names => { :new => 'make', :edit => 'change' } /photos/make /photos/1/change scope :path_names => { :new => "make" } do   # rest of your routes end ROR Lab.
  • 24.
    Customizing Resourceful Routes scope "admin"do   resources :photos, :as => "admin_photos" end   resources :photos admin_photos_path, new_admin_photo_path scope "admin", :as => "admin" do   resources :photos, :accounts end   admin_photos_path and admin_accounts_path resources :photos, :accounts /admin/photos and /admin/accounts ROR Lab.
  • 25.
    Customizing Resourceful Routes scope ":username"do   resources :posts /bob/posts/1 params[:username] ROR Lab.
  • 26.
    Customizing Resourceful Routes resources :photos,:only => [:index, :show] resources :photos, :except => :destroy ROR Lab.
  • 27.
    Customizing Resourceful Routes scope(:path_names =>{ :new => "neu", :edit => "bearbeiten" }) do   resources :categories, :path => "kategorien" end ROR Lab.
  • 28.
    Customizing Resourceful Routes resources :magazinesdo   resources :ads, :as => 'periodical_ads' end magazine_periodical_ads_url edit_magazine_periodical_ad_path ROR Lab.
  • 29.