Rails controllerの複数形と単数形

Ruby on Rails の勉強を始めたころ、なぜここは単数形で名前をつけるのか、なぜここは複数形にするのか、といったことでいちいち悩みました。最近、RESTful routing の勉強をしていて、controller の名前を単数にしてしまった場合の例が見つからず、「名前つけ直そうか」と焦ったのですが。。

以下、Rails 2.2.2 で確認しました。
config/routes.rb で

  map.resources :channels

と書くと

$ rake routes
              channels GET    /channels                        {:controller=>"channels", :action=>"index"}
    formatted_channels GET    /channels.:format                {:controller=>"channels", :action=>"index"}
                       POST   /channels                        {:controller=>"channels", :action=>"create"}
                       POST   /channels.:format                {:controller=>"channels", :action=>"create"}
           new_channel GET    /channels/new                    {:controller=>"channels", :action=>"new"}
 formatted_new_channel GET    /channels/new.:format            {:controller=>"channels", :action=>"new"}
          edit_channel GET    /channels/:id/edit               {:controller=>"channels", :action=>"edit"}
formatted_edit_channel GET    /channels/:id/edit.:format       {:controller=>"channels", :action=>"edit"}
               channel GET    /channels/:id                    {:controller=>"channels", :action=>"show"}
     formatted_channel GET    /channels/:id.:format            {:controller=>"channels", :action=>"show"}
                       PUT    /channels/:id                    {:controller=>"channels", :action=>"update"}
                       PUT    /channels/:id.:format            {:controller=>"channels", :action=>"update"}
                       DELETE /channels/:id                    {:controller=>"channels", :action=>"destroy"}
                       DELETE /channels/:id.:format            {:controller=>"channels", :action=>"destroy"}

となり、view で <%= link_to "index", channels_path %> などと書けます。

たまたま controller を channel のように単数形にしてしまうとどうなるのか、試してみました。

  map.resources :channel
$ rake routes
          channel_index GET    /channel                         {:controller=>"channel", :action=>"index"}
formatted_channel_index GET    /channel.:format                 {:controller=>"channel", :action=>"index"}
                        POST   /channel                         {:controller=>"channel", :action=>"create"}
                        POST   /channel.:format                 {:controller=>"channel", :action=>"create"}
            new_channel GET    /channel/new                     {:controller=>"channel", :action=>"new"}
  formatted_new_channel GET    /channel/new.:format             {:controller=>"channel", :action=>"new"}
           edit_channel GET    /channel/:id/edit                {:controller=>"channel", :action=>"edit"}
 formatted_edit_channel GET    /channel/:id/edit.:format        {:controller=>"channel", :action=>"edit"}
                channel GET    /channel/:id                     {:controller=>"channel", :action=>"show"}
      formatted_channel GET    /channel/:id.:format             {:controller=>"channel", :action=>"show"}
                        PUT    /channel/:id                     {:controller=>"channel", :action=>"update"}
                        PUT    /channel/:id.:format             {:controller=>"channel", :action=>"update"}
                        DELETE /channel/:id                     {:controller=>"channel", :action=>"destroy"}
                        DELETE /channel/:id.:format             {:controller=>"channel", :action=>"destroy"}

のようになりました。
link_to 'xxx', channels_path が channel_index_path のようになり、多少名前が冗長にはなりますが、無事に RESTful routing に移行できそうです。

  • 2009-07-22 追記:rails 2.x の scaffold が生成するのは「複数形の名前が付けられたコントローラ」なので、scaffold 流に揃えたほうがよさそうですね。