2011年4月8日金曜日

Rails3 routes

やはり触れることになってしまった routes.rb。これを期に意味不明な部分を理解しよう。
勉強して得られた情報をメモ。

routes.rb にて生成される URL については rake routes コマンドにて確認できる。
記載する出力結果はすべて rake routes にて出力された結果です。


match(path, options={})


route 師弟の基本。アクセス可能な path を指定してやることで、HTTP リクエストを処理できるようになる。
match "foo/bar"
# foo_bar  /foo/bar(.:format) {:controller=>"foo", :action=>"bar"}
path は必ず一つ以上 "/" を入れてやる必要がある。
  • match "foo" => NG
  • match "foo/bar" => :controller=>"foo", :action=>"bar"
  • match "foo/bar/buz" => :controller=>"foo/bar", :action=>"buz"
"/" を複数指定した場合、controller の namespace を表すように解釈される。

以下指定可能オプション。(全部じゃないかも)

:controller
:action
動作させるコントローラとアクションを指定する。
必ずセットで指定する。
path に "/" を入れなくてもこのオプションを指定してやれば解釈されるようになる。
match "foo", :controller=>"foo", :action=>"bar"
# foo  /foo(.:format)  {:controller=>"foo", :action=>"bar"}
:to :controller, :action の短縮形。"#" でコントローラとアクションを区切って記述する。
match "foo", :to=>"foo#bar"
:to の記述自体を短縮する方法もある。
match "foo"=>"foo#bar"
:via HTTP メソッドを付加する。
match "foo/bar", :via=>:get
# foo_bar GET /foo/bar(.:format) {:controller=>"foo", :action=>"bar"}
:as ルート名を指定する。ルート名は form_tag や link_to などで指定できる名前で、その名前に該当する path を生成して form を作成してくれる。
match "foo/bar", :as=>"bar"
# bar GET /foo/bar(.:format) {:controller=>"foo", :action=>"bar"}


get

post

put

delete


match の :via オプションの HTTPHelper メソッド。
指定可能なパラメータは match と同様。なので
get "foo/bar", :via=>:delete
なんていうふざけた記述もちゃんと解釈してくれる。(HTTP メソッドは GET になります)


resource

resources


Rails のルールに従って RESTful 的な URL を自動で生成してくれる有名なメソッド。
単数形の場合は resource を、複数形の場合は resources を利用する。*指定する名前も単数形、複数形を意識して指定してやる。(resource :foo, resources :foos)
二つの違いは一覧出力 URL (scaffold 生成時の index アクション) の有無の違い。

以下指定可能オプション。(全部じゃないかも)
:as ルート名に利用する別名。
resource "foo", :as=>"bar"
#   bar POST   /foo(.:format)      {:action=>"create", :controller=>"foos"}
# new_bar GET    /foo/new(.:format)  {:action=>"new", :controller=>"foos"}
#edit_bar GET    /foo/edit(.:format) {:action=>"edit", :controller=>"foos"}
#       GET    /foo(.:format)      {:action=>"show", :controller=>"foos"}
#       PUT    /foo(.:format)      {:action=>"update", :controller=>"foos"}
#       DELETE /foo(.:format)      {:action=>"destroy", :controller=>"foos"}
:controller 処理するコントローラを指定する。
アクション名は Rails ルールに従う必要がある。
resource "foo", :controller=>"bar"
#   foo POST   /foo(.:format)      {:action=>"create", :controller=>"bar"}
# new_foo GET    /foo/new(.:format)  {:action=>"new", :controller=>"bar"}
#edit_foo GET    /foo/edit(.:format) {:action=>"edit", :controller=>"bar"}
#        GET    /foo(.:format)      {:action=>"show", :controller=>"bar"}
#        PUT    /foo(.:format)      {:action=>"update", :controller=>"bar"}
#        DELETE /foo(.:format)      {:action=>"destroy", :controller=>"bar"}
:path URL を置き換える。
resource "foo", :path=>"b/a"
#   foo POST   /b/a(.:format)  {:action=>"create",:controller=>"foos"}
# new_foo GET    /b/a/new(.:format) {:action=>"new",:controller=>"foos"}
#edit_foo GET    /b/a(.:format)     {:action=>"edit",:controller=>"foos"}
#         GET    /b/a(.:format)     {:action=>"show",:controller=>"foos"}
#         PUT    /b/a(.:format)     {:action=>"update",:controller=>"foos"}
#         DELETE /b/a(.:format)     {:action=>"destroy",:controller=>"foos"}
:only 作成される URL を絞り込む。指定されたアクションのみ URL が生成される。
resources "foo", :only=>["index"]
# foo_index GET /foo(.:format) {:action=>"index", :controller=>"foo"}
:except 作成する URL を削除する。指定されたアクションは URL が生成されない。
こう書けば、:only と同じ結果になる。
resources "foo", :except=>["create","edit","new","show","update","destroy"]
# foo_index GET /foo(.:format) {:action=>"index", :controller=>"foo"}
:module controller に namespace を付加する。
resources "foo", :only=>["index"], :module=>"module"
# foo_index GET /foo(.:format) {:action=>"index", :controller=>"module/foo"}

has_many の関係を定義する場合、block を渡して resources を定義する。

resources "foo" do
  resources "bar"
end


scope


URL に namespace を付けるイメージ。controller には付かない所がポイント。
scope "scope" do
  resources "foo", :only=>["index"]
end
# foo_index GET /scope/foo(.:format) {:action=>"index", :controller=>"foo"}

block 内に match(path) を指定した場合、namespace として扱われる。(controller にも scope が付加される)
scope "scope" do
  match "foo/bar"
end
# foo_bar  /scope/foo/bar(.:format) {:controller=>"scope/foo", :action=>"bar"}
:to オプションを付けて controller と action を宣言してやれば、URL のみ対象となる。

以下指定可能オプション。(全部じゃないかも)
:module controller の namespace を指定する。
scope "sco", :module=>"mod" do
  resources "foo", :only=>["index"]
end
# foo_index GET /sco/foo(.:format) {:action=>"index", :controller=>"mod/foo"}
:as ルート名に prefix を付加する。resources は別名に置き換えるがこっちは付加する。
scope "sco", :as=>"as" do
  resources "foo", :only=>["index"]
end
# as_foo_index GET /sco/foo(.:format) {:action=>"index", :controller=>"foo"}

controller のみに namespace を付加したい場合、
scope :module=>"module" do
  resources "foo", :only=>["index"]
end
としてやればよい。


namespace


URL 名, URL と controller に(要は全てに) namespace を付加する。
namespace "nspe" do
  resources "foo", :only=>["index"]
end
# nspe_foo_index GET /nspe/foo(.:format) {:action=>"index", :controller=>"nspe/foo"}

以下指定可能オプション。(全部じゃないかも)
:as ルート名に prefix を付加する。動きは scope と同じ。


controller


controller を一括で指定できる。
controller "foo" do
  get "search"
end
# search GET /search(.:format) {:action=>"search", :controller=>"foo"}
match でエラーになるパターン ("/" なし) も controller を付加してくれてちゃんと動く。
優先度は block 内のメソッドの方が高く、resources "bar" や match "bar/buz" など指定しても適用されない。(:controller=>"bar"になる)

以下指定可能オプション。(全部じゃないかも)
:path URL に prefix を付加する。デフォルトでは controller 名は URL に付加されないので、必要な場合は指定する。
controller "foo", :path=>"foo" do
  get "index"
end
# index GET /foo/index(.:format) {:action=>"index", :controller=>"foo"}
:as ルート名に prefix を付加する。動きは scope と同じ。


root


root ("/") にアクセスされた際の routes を指定する。
root :to=>"foo#bar"
# root  /(.:format) {:controller=>"foo", :action=>"bar"}



長文でダラダラとまとめたが、まだまだ全部でない感じがする。(ソース見た感じ)
けど、これで routes への恐怖心が若干薄まった感がある。