The 13th Round of ROR Lab.


   Rails Routing
from the Outside In
         (1)
         May 26th, 2012

         Hyoseong Choi
           ROR Lab.
Rails Router
   http://blog.org


  _path           _url




   A Controller Action



                         ROR Lab.
$ rake routes

    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
 new_post GET    /posts/new(.:format)      posts#new
edit_post GET    /posts/:id/edit(.:format) posts#edit
     post GET    /posts/:id(.:format)      posts#show
          PUT    /posts/:id(.:format)      posts#update
          DELETE /posts/:id(.:format)      posts#destroy




                                                        ROR Lab.
An Incoming Request

    GET /posts/17



         ?
                    ROR Lab.
Generating
   paths & URLs
@patient = Patient.find(17)




<%= link_to "Patient Record", patient_path(@patient) %>




         URL = host + port + PATH

                                                          ROR Lab.
Resource Routing
• Action 7 brothers
• index / show / new / edit
  create / update / destroy
                           in config/routes.rb

   resources :photos



“sine” - show, index, new, edit
                                            ROR Lab.
An Incoming Request

  DELETE /posts/17



        ?
                     ROR Lab.
HTTP verbs
                         new      create      C
           • POST
                                              R
           • GET
                        index     show
URL
           • PUT        update                U
           • DELETE
                        destroy               D
Note : routing orders                      ROR Lab.
Multiple Resources

 resources :photos
 resources :books
 resources :videos




 resources :photos, :books, :videos




                                      ROR Lab.
Singular Resources

 match "profile" => "users#show"




 resource :geocoder




                                  ROR Lab.
Singular Resources
  resource :geocoder




 Action 6 brothers     no index action




                                         ROR Lab.
Controller
      Namespacing
Admin:: module prefix

   namespace :admin do
     resources :posts, :comments




   scope :module => "admin" do
     resources :posts, :comments




   scope "/admin" do
     resources :posts, :comments



                                   ROR Lab.
Controller
   Namespacing
namespace :admin do
  resources :posts, :comments
                                Admin:: module prefix




                                                 ROR Lab.
Controller
   Namespacing
scope :module => "admin" do
  resources :posts, :comments
                                Admin:: module prefix




                                                 ROR Lab.
Controller
   Namespacing
scope "/admin" do
  resources :posts, :comments
                           without Admin:: module prefix




                                                    ROR Lab.
Nested Resources
 class Magazine < ActiveRecord::Base
   has_many :ads
 end
  
 class Ad < ActiveRecord::Base
   belongs_to :magazine




 resources :magazines do
   resources :ads




                                       ROR Lab.
Nested Resources
         resources :magazines do
           resources :ads




                             ROR Lab.
Paths & URLs
     From Objects
<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>




<%= link_to "Ad details", url_for([@magazine, @ad]) %>




<%= link_to "Ad details", [@magazine, @ad] %>




                                                                ROR Lab.
More RESTful
       Actions
• Member Routes
 resources :photos do
   member do
     get 'preview'
   end




 URL : /photos/1/preview with GET

 Named Routes
     : preview_photo_url and preview_photo_path



                                                  ROR Lab.
More RESTful
       Actions
• Collection Routes
 resources :photos do
   collection do
     get 'search'
   end




 URL : /photos/search with GET

 Named Routes
     : search_photos_url and search_photos_path



                                                  ROR Lab.
감사합니다.

Routing 1, Season 1

  • 1.
    The 13th Roundof ROR Lab. Rails Routing from the Outside In (1) May 26th, 2012 Hyoseong Choi ROR Lab.
  • 2.
    Rails Router http://blog.org _path _url A Controller Action ROR Lab.
  • 3.
    $ rake routes posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy ROR Lab.
  • 4.
    An Incoming Request GET /posts/17 ? ROR Lab.
  • 5.
    Generating paths & URLs @patient = Patient.find(17) <%= link_to "Patient Record", patient_path(@patient) %> URL = host + port + PATH ROR Lab.
  • 6.
    Resource Routing • Action7 brothers • index / show / new / edit create / update / destroy in config/routes.rb resources :photos “sine” - show, index, new, edit ROR Lab.
  • 7.
    An Incoming Request DELETE /posts/17 ? ROR Lab.
  • 8.
    HTTP verbs new create C • POST R • GET index show URL • PUT update U • DELETE destroy D Note : routing orders ROR Lab.
  • 9.
    Multiple Resources resources:photos resources :books resources :videos resources :photos, :books, :videos ROR Lab.
  • 10.
    Singular Resources match"profile" => "users#show" resource :geocoder ROR Lab.
  • 11.
    Singular Resources resource :geocoder Action 6 brothers no index action ROR Lab.
  • 12.
    Controller Namespacing Admin:: module prefix namespace :admin do   resources :posts, :comments scope :module => "admin" do   resources :posts, :comments scope "/admin" do   resources :posts, :comments ROR Lab.
  • 13.
    Controller Namespacing namespace :admin do   resources :posts, :comments Admin:: module prefix ROR Lab.
  • 14.
    Controller Namespacing scope :module => "admin" do   resources :posts, :comments Admin:: module prefix ROR Lab.
  • 15.
    Controller Namespacing scope "/admin" do   resources :posts, :comments without Admin:: module prefix ROR Lab.
  • 16.
    Nested Resources classMagazine < ActiveRecord::Base   has_many :ads end   class Ad < ActiveRecord::Base   belongs_to :magazine resources :magazines do   resources :ads ROR Lab.
  • 17.
    Nested Resources resources :magazines do   resources :ads ROR Lab.
  • 18.
    Paths & URLs From Objects <%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %> <%= link_to "Ad details", url_for([@magazine, @ad]) %> <%= link_to "Ad details", [@magazine, @ad] %> ROR Lab.
  • 19.
    More RESTful Actions • Member Routes resources :photos do   member do     get 'preview'   end URL : /photos/1/preview with GET Named Routes : preview_photo_url and preview_photo_path ROR Lab.
  • 20.
    More RESTful Actions • Collection Routes resources :photos do   collection do     get 'search'   end URL : /photos/search with GET Named Routes : search_photos_url and search_photos_path ROR Lab.
  • 21.