No. |
名前付きルート名 |
メソッド |
URLパス書式 |
処理されるコントローラー、アクション |
URL例 |
1 |
slip_journals |
GET |
/slips/:slip_id/journals |
{:controller=>"journals", :action=>"index"} |
http://XXXX.XXX/slips/1/journals |
2 |
formatted_slip_journals |
GET |
/slips/:slip_id/journals.:format |
{:controller=>"journals", :action=>"index"} |
http://XXXX.XXX/slips/1/journals.xml |
3 |
|
POST |
/slips/:slip_id/journals |
{:controller=>"journals", :action=>"create"} |
http://XXXX.XXX/slips/1/journals |
4 |
|
POST |
/slips/:slip_id/journals.:format |
{:controller=>"journals", :action=>"create"} |
http://XXXX.XXX/slips/1/journals.xml |
5 |
new_slip_journal |
GET |
/slips/:slip_id/journals/new |
{:controller=>"journals", :action=>"new"} |
http://XXXX.XXX/slips/1/journals/new |
6 |
formatted_new_slip_journal |
GET |
/slips/:slip_id/journals/new.:format |
{:controller=>"journals", :action=>"new"} |
http://XXXX.XXX/slips/1/journals/new.xml |
7 |
edit_slip_journal |
GET |
/slips/:slip_id/journals/:id/edit |
{:controller=>"journals", :action=>"edit"} |
http://XXXX.XXX/slips/1/journals/2/edit |
8 |
formatted_edit_slip_journal |
GET |
/slips/:slip_id/journals/:id/edit.:format |
{:controller=>"journals", :action=>"edit"} |
http://XXXX.XXX/slips/1/journals/2/edit.xml |
9 |
slip_journal |
GET |
/slips/:slip_id/journals/:id |
{:controller=>"journals", :action=>"show"} |
http://XXXX.XXX/slips/1/journals/2 |
10 |
formatted_slip_journal |
GET |
/slips/:slip_id/journals/:id.:format |
{:controller=>"journals", :action=>"show"} |
http://XXXX.XXX/slips/1/journals/2.xml |
11 |
|
PUT |
/slips/:slip_id/journals/:id |
{:controller=>"journals", :action=>"update"} |
http://XXXX.XXX/slips/1/journals/2 |
12 |
|
PUT |
/slips/:slip_id/journals/:id.:format |
{:controller=>"journals", :action=>"update"} |
http://XXXX.XXX/slips/1/journals/2.xml |
13 |
|
DELETE |
/slips/:slip_id/journals/:id |
{:controller=>"journals", :action=>"destroy"} |
http://XXXX.XXX/slips/1/journals/2 |
14 |
|
DELETE |
/slips/:slip_id/journals/:id.:format |
{:controller=>"journals", :action=>"destroy"} |
http://XXXX.XXX/slips/1/journals/2.xml |
|
:controller
- 通常は、map.resources :slipsでは、リクエストはslipsコントローラーで処理される。
- ところが、:controller=>'test'を追記することで、testコントローラーで処理されることになる。
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :slips, :controller=>'test'
end
# rake routesの結果
slips GET /slips {:controller=>"test", :action=>"index"}
formatted_slips GET /slips.:format {:controller=>"test", :action=>"index"}
POST /slips {:controller=>"test", :action=>"create"}
POST /slips.:format {:controller=>"test", :action=>"create"}
new_slip GET /slips/new {:controller=>"test", :action=>"new"}
formatted_new_slip GET /slips/new.:format {:controller=>"test", :action=>"new"}
edit_slip GET /slips/:id/edit {:controller=>"test", :action=>"edit"}
formatted_edit_slip GET /slips/:id/edit.:format {:controller=>"test", :action=>"edit"}
slip GET /slips/:id {:controller=>"test", :action=>"show"}
formatted_slip GET /slips/:id.:format {:controller=>"test", :action=>"show"}
PUT /slips/:id {:controller=>"test", :action=>"update"}
PUT /slips/:id.:format {:controller=>"test", :action=>"update"}
DELETE /slips/:id {:controller=>"test", :action=>"destroy"}
DELETE /slips/:id.:format {:controller=>"test", :action=>"destroy"}
:requirements
- パラメーターの書式が正規表現にマッチした場合だけ、そのルート規則が適用される。
- 以下の例では、rake routesの結果に変化は無いが、:idを含むURL書式は、指定されたHTTPメソッドかつ、:idが1桁の数字の場合だけ、ルート規則が適用されることになる。(idが2桁以上、または数字以外では適用されない。その場合、デフォルトのmap.connect ':controller/:action/:id'が無い状況ではRouting Errorになる。)
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :slips, :requirements=>{:id=>/\d/}
end
# rake routesの結果
slips GET /slips {:controller=>"slips", :action=>"index"}
formatted_slips GET /slips.:format {:controller=>"slips", :action=>"index"}
POST /slips {:controller=>"slips", :action=>"create"}
POST /slips.:format {:controller=>"slips", :action=>"create"}
new_slip GET /slips/new {:controller=>"slips", :action=>"new"}
formatted_new_slip GET /slips/new.:format {:controller=>"slips", :action=>"new"}
edit_slip GET /slips/:id/edit {:controller=>"slips", :action=>"edit"}
formatted_edit_slip GET /slips/:id/edit.:format {:controller=>"slips", :action=>"edit"}
slip GET /slips/:id {:controller=>"slips", :action=>"show"}
formatted_slip GET /slips/:id.:format {:controller=>"slips", :action=>"show"}
PUT /slips/:id {:controller=>"slips", :action=>"update"}
PUT /slips/:id.:format {:controller=>"slips", :action=>"update"}
DELETE /slips/:id {:controller=>"slips", :action=>"destroy"}
DELETE /slips/:id.:format {:controller=>"slips", :action=>"destroy"}
:path_prefix、:name_prefix
- URLパス書式に'/slips/:slip_id'を付加する。
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :journals, :path_prefix=>'/slips/:slip_id'
end
# rake routesの結果
journals GET /slips/:slip_id/journals {:controller=>"journals", :action=>"index"}
formatted_journals GET /slips/:slip_id/journals.:format {:controller=>"journals", :action=>"index"}
POST /slips/:slip_id/journals {:controller=>"journals", :action=>"create"}
POST /slips/:slip_id/journals.:format {:controller=>"journals", :action=>"create"}
new_journal GET /slips/:slip_id/journals/new {:controller=>"journals", :action=>"new"}
formatted_new_journal GET /slips/:slip_id/journals/new.:format {:controller=>"journals", :action=>"new"}
edit_journal GET /slips/:slip_id/journals/:id/edit {:controller=>"journals", :action=>"edit"}
formatted_edit_journal GET /slips/:slip_id/journals/:id/edit.:format {:controller=>"journals", :action=>"edit"}
journal GET /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"show"}
formatted_journal GET /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"show"}
PUT /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"update"}
PUT /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"update"}
DELETE /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"destroy"}
DELETE /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"destroy"}
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :journals, :name_prefix=>'slip_'
end
# rake routesの結果
slip_journals GET /journals {:controller=>"journals", :action=>"index"}
formatted_slip_journals GET /journals.:format {:controller=>"journals", :action=>"index"}
POST /journals {:controller=>"journals", :action=>"create"}
POST /journals.:format {:controller=>"journals", :action=>"create"}
new_slip_journal GET /journals/new {:controller=>"journals", :action=>"new"}
formatted_new_slip_journal GET /journals/new.:format {:controller=>"journals", :action=>"new"}
edit_slip_journal GET /journals/:id/edit {:controller=>"journals", :action=>"edit"}
formatted_edit_slip_journal GET /journals/:id/edit.:format {:controller=>"journals", :action=>"edit"}
slip_journal GET /journals/:id {:controller=>"journals", :action=>"show"}
formatted_slip_journal GET /journals/:id.:format {:controller=>"journals", :action=>"show"}
PUT /journals/:id {:controller=>"journals", :action=>"update"}
PUT /journals/:id.:format {:controller=>"journals", :action=>"update"}
DELETE /journals/:id {:controller=>"journals", :action=>"destroy"}
DELETE /journals/:id.:format {:controller=>"journals", :action=>"destroy"}
- :path_prefixと:name_prefixの両方を使ったら...それってmap.resources :slips, :has_many=>:journalsと同じこと?...どうも同じことのようだ。
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :journals, :path_prefix=>'/slips/:slip_id', :name_prefix=>'slip_'
end
# rake routesの結果
slip_journals GET /slips/:slip_id/journals {:controller=>"journals", :action=>"index"}
formatted_slip_journals GET /slips/:slip_id/journals.:format {:controller=>"journals", :action=>"index"}
POST /slips/:slip_id/journals {:controller=>"journals", :action=>"create"}
POST /slips/:slip_id/journals.:format {:controller=>"journals", :action=>"create"}
new_slip_journal GET /slips/:slip_id/journals/new {:controller=>"journals", :action=>"new"}
formatted_new_slip_journal GET /slips/:slip_id/journals/new.:format {:controller=>"journals", :action=>"new"}
edit_slip_journal GET /slips/:slip_id/journals/:id/edit {:controller=>"journals", :action=>"edit"}
formatted_edit_slip_journal GET /slips/:slip_id/journals/:id/edit.:format {:controller=>"journals", :action=>"edit"}
slip_journal GET /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"show"}
formatted_slip_journal GET /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"show"}
PUT /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"update"}
PUT /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"update"}
DELETE /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"destroy"}
DELETE /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"destroy"}
- 上記を応用すれば、こんなことも出来るらしい...。名前付きルート名から"slip_"を外してしまう。
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :slips do |slip|
slip.resources :journals, :name_prefix=>""
end
end
# rake routesの結果
journals GET /slips/:slip_id/journals {:controller=>"journals", :action=>"index"}
formatted_journals GET /slips/:slip_id/journals.:format {:controller=>"journals", :action=>"index"}
POST /slips/:slip_id/journals {:controller=>"journals", :action=>"create"}
POST /slips/:slip_id/journals.:format {:controller=>"journals", :action=>"create"}
new_journal GET /slips/:slip_id/journals/new {:controller=>"journals", :action=>"new"}
formatted_new_journal GET /slips/:slip_id/journals/new.:format {:controller=>"journals", :action=>"new"}
edit_journal GET /slips/:slip_id/journals/:id/edit {:controller=>"journals", :action=>"edit"}
formatted_edit_journal GET /slips/:slip_id/journals/:id/edit.:format {:controller=>"journals", :action=>"edit"}
journal GET /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"show"}
formatted_journal GET /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"show"}
PUT /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"update"}
PUT /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"update"}
DELETE /slips/:slip_id/journals/:id {:controller=>"journals", :action=>"destroy"}
DELETE /slips/:slip_id/journals/:id.:format {:controller=>"journals", :action=>"destroy"}
- すると..."slip_"が無い名前付きルートで以下のように指定できる。(@slipはidが1のSlipインスタンス、@journalはidが9のJournaインスタンスの場合)
- <%= journals_path(@slip) %> -----------------> "/slips/1/journals"
- <%= edit_journal_path(@slip, @journal) %> ---> "/slips/1/journals/9/edit"
:singular
- 通常はmap.resources :slipsのように複数形で指定すると...自動変換された単数形の名前付きルートが利用できるようになる。(このページの一番最初の基本ルート設定で確認できる。例:slip_url(@slip)、new_slip_url(@slip)、edit_slip_url(@slip)。)
- :singularを指定すると、単数形の名前付きルートは:singularで指定した文字列に変換される。(下記の例は不自然過ぎるが...おそらく、単数形・複数形が不規則に変化する単語で、自動変換が対応しない時に活躍しそう。)
# ルート設定: config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.resources :slips, :singular=>:test
end
# rake routesの結果
slips GET /slips {:controller=>"slips", :action=>"index"}
formatted_slips GET /slips.:format {:controller=>"slips", :action=>"index"}
POST /slips {:controller=>"slips", :action=>"create"}
POST /slips.:format {:controller=>"slips", :action=>"create"}
new_test GET /slips/new {:controller=>"slips", :action=>"new"}
formatted_new_test GET /slips/new.:format {:controller=>"slips", :action=>"new"}
edit_test GET /slips/:id/edit {:controller=>"slips", :action=>"edit"}
formatted_edit_test GET /slips/:id/edit.:format {:controller=>"slips", :action=>"edit"}
test GET /slips/:id {:controller=>"slips", :action=>"show"}
formatted_test GET /slips/:id.:format {:controller=>"slips", :action=>"show"}
PUT /slips/:id {:controller=>"slips", :action=>"update"}
PUT /slips/:id.:format {:controller=>"slips", :action=>"update"}
DELETE /slips/:id {:controller=>"slips", :action=>"destroy"}
DELETE /slips/:id.:format {:controller=>"slips", :action=>"destroy"}