読者です 読者をやめる 読者になる 読者になる

Railsのroutesのマッピング

RailsがどのようにRotingするのかをまとめてみた。

map.resource

map.resource :products

               products POST   /products              {:controller=>"products", :action=>"create"}
     formatted_products POST   /products.:format      {:controller=>"products", :action=>"create"}
           new_products GET    /products/new          {:controller=>"products", :action=>"new"}
 formatted_new_products GET    /products/new.:format  {:controller=>"products", :action=>"new"}
          edit_products GET    /products/edit         {:controller=>"products", :action=>"edit"}
formatted_edit_products GET    /products/edit.:format {:controller=>"products", :action=>"edit"}
                        GET    /products              {:controller=>"products", :action=>"show"}
                        GET    /products.:format      {:controller=>"products", :action=>"show"}
                        PUT    /products              {:controller=>"products", :action=>"update"}
                        PUT    /products.:format      {:controller=>"products", :action=>"update"}
                        DELETE /products              {:controller=>"products", :action=>"destroy"}
                        DELETE /products.:format      {:controller=>"products", :action=>"destroy"}

map.resources

              products GET    /products                  {:controller=>"products", :action=>"index"}
    formatted_products GET    /products.:format          {:controller=>"products", :action=>"index"}
                       POST   /products                  {:controller=>"products", :action=>"create"}
                       POST   /products.:format          {:controller=>"products", :action=>"create"}
           new_product GET    /products/new              {:controller=>"products", :action=>"new"}
 formatted_new_product GET    /products/new.:format      {:controller=>"products", :action=>"new"}
          edit_product GET    /products/:id/edit         {:controller=>"products", :action=>"edit"}
formatted_edit_product GET    /products/:id/edit.:format {:controller=>"products", :action=>"edit"}
               product GET    /products/:id              {:controller=>"products", :action=>"show"}
     formatted_product GET    /products/:id.:format      {:controller=>"products", :action=>"show"}
                       PUT    /products/:id              {:controller=>"products", :action=>"update"}
                       PUT    /products/:id.:format      {:controller=>"products", :action=>"update"}
                       DELETE /products/:id              {:controller=>"products", :action=>"destroy"}
                       DELETE /products/:id.:format      {:controller=>"products", :action=>"destroy"}

map.resources - collection

map.resources :products, :collection => { :sold => :get }

(in /home/dann/workdir/lang/ruby/angelos)
          sold_products GET    /products/sold             {:controller=>"products", :action=>"sold"}
formatted_sold_products GET    /products/sold.:format     {:controller=>"products", :action=>"sold"}
               products GET    /products                  {:controller=>"products", :action=>"index"}
     formatted_products GET    /products.:format          {:controller=>"products", :action=>"index"}
                        POST   /products                  {:controller=>"products", :action=>"create"}
                        POST   /products.:format          {:controller=>"products", :action=>"create"}
            new_product GET    /products/new              {:controller=>"products", :action=>"new"}
  formatted_new_product GET    /products/new.:format      {:controller=>"products", :action=>"new"}
           edit_product GET    /products/:id/edit         {:controller=>"products", :action=>"edit"}
 formatted_edit_product GET    /products/:id/edit.:format {:controller=>"products", :action=>"edit"}
                product GET    /products/:id              {:controller=>"products", :action=>"show"}
      formatted_product GET    /products/:id.:format      {:controller=>"products", :action=>"show"}
                        PUT    /products/:id              {:controller=>"products", :action=>"update"}
                        PUT    /products/:id.:format      {:controller=>"products", :action=>"update"}
                        DELETE /products/:id              {:controller=>"products", :action=>"destroy"}
                        DELETE /products/:id.:format      {:controller=>"products", :action=>"destroy"}

map.resources - member

map.resources :products, :member => { :short => :get, :toggle => :post }

                products GET    /products                    {:controller=>"products", :action=>"index"}
      formatted_products GET    /products.:format            {:controller=>"products", :action=>"index"}
                         POST   /products                    {:controller=>"products", :action=>"create"}
                         POST   /products.:format            {:controller=>"products", :action=>"create"}
             new_product GET    /products/new                {:controller=>"products", :action=>"new"}
   formatted_new_product GET    /products/new.:format        {:controller=>"products", :action=>"new"}
          toggle_product POST   /products/:id/toggle         {:controller=>"products", :action=>"toggle"}
formatted_toggle_product POST   /products/:id/toggle.:format {:controller=>"products", :action=>"toggle"}
            edit_product GET    /products/:id/edit           {:controller=>"products", :action=>"edit"}
  formatted_edit_product GET    /products/:id/edit.:format   {:controller=>"products", :action=>"edit"}
           short_product GET    /products/:id/short          {:controller=>"products", :action=>"short"}
 formatted_short_product GET    /products/:id/short.:format  {:controller=>"products", :action=>"short"}
                 product GET    /products/:id                {:controller=>"products", :action=>"show"}
       formatted_product GET    /products/:id.:format        {:controller=>"products", :action=>"show"}
                         PUT    /products/:id                {:controller=>"products", :action=>"update"}
                         PUT    /products/:id.:format        {:controller=>"products", :action=>"update"}
                         DELETE /products/:id                {:controller=>"products", :action=>"destroy"}
                         DELETE /products/:id.:format        {:controller=>"products", :action=>"destroy"}

map.resources has_many, has_one

map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller

                      products GET    /products                                       {:controller=>"products", :action=>"index"}
            formatted_products GET    /products.:format                               {:controller=>"products", :action=>"index"}
                               POST   /products                                       {:controller=>"products", :action=>"create"}
                               POST   /products.:format                               {:controller=>"products", :action=>"create"}
                   new_product GET    /products/new                                   {:controller=>"products", :action=>"new"}
         formatted_new_product GET    /products/new.:format                           {:controller=>"products", :action=>"new"}
                  edit_product GET    /products/:id/edit                              {:controller=>"products", :action=>"edit"}
        formatted_edit_product GET    /products/:id/edit.:format                      {:controller=>"products", :action=>"edit"}
                       product GET    /products/:id                                   {:controller=>"products", :action=>"show"}
             formatted_product GET    /products/:id.:format                           {:controller=>"products", :action=>"show"}
                               PUT    /products/:id                                   {:controller=>"products", :action=>"update"}
                               PUT    /products/:id.:format                           {:controller=>"products", :action=>"update"}
                               DELETE /products/:id                                   {:controller=>"products", :action=>"destroy"}
                               DELETE /products/:id.:format                           {:controller=>"products", :action=>"destroy"}
              product_comments GET    /products/:product_id/comments                  {:controller=>"comments", :action=>"index"}
    formatted_product_comments GET    /products/:product_id/comments.:format          {:controller=>"comments", :action=>"index"}
                               POST   /products/:product_id/comments                  {:controller=>"comments", :action=>"create"}
                               POST   /products/:product_id/comments.:format          {:controller=>"comments", :action=>"create"}
           new_product_comment GET    /products/:product_id/comments/new              {:controller=>"comments", :action=>"new"}
 formatted_new_product_comment GET    /products/:product_id/comments/new.:format      {:controller=>"comments", :action=>"new"}
          edit_product_comment GET    /products/:product_id/comments/:id/edit         {:controller=>"comments", :action=>"edit"}
formatted_edit_product_comment GET    /products/:product_id/comments/:id/edit.:format {:controller=>"comments", :action=>"edit"}
               product_comment GET    /products/:product_id/comments/:id              {:controller=>"comments", :action=>"show"}
     formatted_product_comment GET    /products/:product_id/comments/:id.:format      {:controller=>"comments", :action=>"show"}
                               PUT    /products/:product_id/comments/:id              {:controller=>"comments", :action=>"update"}
                               PUT    /products/:product_id/comments/:id.:format      {:controller=>"comments", :action=>"update"}
                               DELETE /products/:product_id/comments/:id              {:controller=>"comments", :action=>"destroy"}
                               DELETE /products/:product_id/comments/:id.:format      {:controller=>"comments", :action=>"destroy"}
                 product_sales GET    /products/:product_id/sales                     {:controller=>"sales", :action=>"index"}
       formatted_product_sales GET    /products/:product_id/sales.:format             {:controller=>"sales", :action=>"index"}
                               POST   /products/:product_id/sales                     {:controller=>"sales", :action=>"create"}
                               POST   /products/:product_id/sales.:format             {:controller=>"sales", :action=>"create"}
              new_product_sale GET    /products/:product_id/sales/new                 {:controller=>"sales", :action=>"new"}
    formatted_new_product_sale GET    /products/:product_id/sales/new.:format         {:controller=>"sales", :action=>"new"}
             edit_product_sale GET    /products/:product_id/sales/:id/edit            {:controller=>"sales", :action=>"edit"}
   formatted_edit_product_sale GET    /products/:product_id/sales/:id/edit.:format    {:controller=>"sales", :action=>"edit"}
                  product_sale GET    /products/:product_id/sales/:id                 {:controller=>"sales", :action=>"show"}
        formatted_product_sale GET    /products/:product_id/sales/:id.:format         {:controller=>"sales", :action=>"show"}
                               PUT    /products/:product_id/sales/:id                 {:controller=>"sales", :action=>"update"}
                               PUT    /products/:product_id/sales/:id.:format         {:controller=>"sales", :action=>"update"}
                               DELETE /products/:product_id/sales/:id                 {:controller=>"sales", :action=>"destroy"}

                product_seller POST   /products/:product_id/seller                    {:controller=>"sellers", :action=>"create"}
      formatted_product_seller POST   /products/:product_id/seller.:format            {:controller=>"sellers", :action=>"create"}
            new_product_seller GET    /products/:product_id/seller/new                {:controller=>"sellers", :action=>"new"}
  formatted_new_product_seller GET    /products/:product_id/seller/new.:format        {:controller=>"sellers", :action=>"new"}
           edit_product_seller GET    /products/:product_id/seller/edit               {:controller=>"sellers", :action=>"edit"}
 formatted_edit_product_seller GET    /products/:product_id/seller/edit.:format       {:controller=>"sellers", :action=>"edit"}
                               GET    /products/:product_id/seller                    {:controller=>"sellers", :action=>"show"}
                               GET    /products/:product_id/seller.:format            {:controller=>"sellers", :action=>"show"}
                               PUT    /products/:product_id/seller                    {:controller=>"sellers", :action=>"update"}
                               PUT    /products/:product_id/seller.:format            {:controller=>"sellers", :action=>"update"}
                               DELETE /products/:product_id/seller                    {:controller=>"sellers", :action=>"destroy"}
                               DELETE /products/:product_id/seller.:format            {:controller=>"sellers", :action=>"destroy"}

map.resourcesのネスト

     map.resources :products do |products|
       products.resources :comments
       products.resources :sales, :collection => { :recent => :get }
     end 
                      products GET    /products                                       {:controller=>"products", :action=>"index"}
            formatted_products GET    /products.:format                               {:controller=>"products", :action=>"index"}
                               POST   /products                                       {:controller=>"products", :action=>"create"}
                               POST   /products.:format                               {:controller=>"products", :action=>"create"}
                   new_product GET    /products/new                                   {:controller=>"products", :action=>"new"}
         formatted_new_product GET    /products/new.:format                           {:controller=>"products", :action=>"new"}
                  edit_product GET    /products/:id/edit                              {:controller=>"products", :action=>"edit"}
        formatted_edit_product GET    /products/:id/edit.:format                      {:controller=>"products", :action=>"edit"}
                       product GET    /products/:id                                   {:controller=>"products", :action=>"show"}
             formatted_product GET    /products/:id.:format                           {:controller=>"products", :action=>"show"}
                               PUT    /products/:id                                   {:controller=>"products", :action=>"update"}
                               PUT    /products/:id.:format                           {:controller=>"products", :action=>"update"}
                               DELETE /products/:id                                   {:controller=>"products", :action=>"destroy"}
                               DELETE /products/:id.:format                           {:controller=>"products", :action=>"destroy"}
              product_comments GET    /products/:product_id/comments                  {:controller=>"comments", :action=>"index"}
    formatted_product_comments GET    /products/:product_id/comments.:format          {:controller=>"comments", :action=>"index"}
                               POST   /products/:product_id/comments                  {:controller=>"comments", :action=>"create"}
                               POST   /products/:product_id/comments.:format          {:controller=>"comments", :action=>"create"}
           new_product_comment GET    /products/:product_id/comments/new              {:controller=>"comments", :action=>"new"}
 formatted_new_product_comment GET    /products/:product_id/comments/new.:format      {:controller=>"comments", :action=>"new"}
          edit_product_comment GET    /products/:product_id/comments/:id/edit         {:controller=>"comments", :action=>"edit"}
formatted_edit_product_comment GET    /products/:product_id/comments/:id/edit.:format {:controller=>"comments", :action=>"edit"}
               product_comment GET    /products/:product_id/comments/:id              {:controller=>"comments", :action=>"show"}
     formatted_product_comment GET    /products/:product_id/comments/:id.:format      {:controller=>"comments", :action=>"show"}
                               PUT    /products/:product_id/comments/:id              {:controller=>"comments", :action=>"update"}
                               PUT    /products/:product_id/comments/:id.:format      {:controller=>"comments", :action=>"update"}
                               DELETE /products/:product_id/comments/:id              {:controller=>"comments", :action=>"destroy"}
                               DELETE /products/:product_id/comments/:id.:format      {:controller=>"comments", :action=>"destroy"}
          recent_product_sales GET    /products/:product_id/sales/recent              {:controller=>"sales", :action=>"recent"}
formatted_recent_product_sales GET    /products/:product_id/sales/recent.:format      {:controller=>"sales", :action=>"recent"}
                 product_sales GET    /products/:product_id/sales                     {:controller=>"sales", :action=>"index"}
       formatted_product_sales GET    /products/:product_id/sales.:format             {:controller=>"sales", :action=>"index"}
                               POST   /products/:product_id/sales                     {:controller=>"sales", :action=>"create"}
                               POST   /products/:product_id/sales.:format             {:controller=>"sales", :action=>"create"}
              new_product_sale GET    /products/:product_id/sales/new                 {:controller=>"sales", :action=>"new"}
    formatted_new_product_sale GET    /products/:product_id/sales/new.:format         {:controller=>"sales", :action=>"new"}
             edit_product_sale GET    /products/:product_id/sales/:id/edit            {:controller=>"sales", :action=>"edit"}
   formatted_edit_product_sale GET    /products/:product_id/sales/:id/edit.:format    {:controller=>"sales", :action=>"edit"}
                  product_sale GET    /products/:product_id/sales/:id                 {:controller=>"sales", :action=>"show"}
        formatted_product_sale GET    /products/:product_id/sales/:id.:format         {:controller=>"sales", :action=>"show"}
                               PUT    /products/:product_id/sales/:id                 {:controller=>"sales", :action=>"update"}
                               PUT    /products/:product_id/sales/:id.:format         {:controller=>"sales", :action=>"update"}
                               DELETE /products/:product_id/sales/:id                 {:controller=>"sales", :action=>"destroy"}
                               DELETE /products/:product_id/sales/:id.:format         {:controller=>"sales", :action=>"destroy"}