普通のRoute

ActionController::Routing::Routes.draw do |map|
map.resources :tasks
end
% rake routes
tasks GET /tasks(.:format) {:action=>"index", :controller=>"tasks"}
POST /tasks(.:format) {:action=>"create", :controller=>"tasks"}
new_task GET /tasks/new(.:format) {:action=>"new", :controller=>"tasks"}
edit_task GET /tasks/:id/edit(.:format) {:action=>"edit", :controller=>"tasks"}
task GET /tasks/:id(.:format) {:action=>"show", :controller=>"tasks"}
PUT /tasks/:id(.:format) {:action=>"update", :controller=>"tasks"}
DELETE /tasks/:id(.:format) {:action=>"destroy", :controller=>"tasks"}

APIの場合、newとeditのページは要らないので絞る。

ActionController::Routing::Routes.draw do |map|
map.resources :tasks, :except => [:new, :edit]
end
% rake routes
tasks GET /tasks(.:format) {:action=>"index", :controller=>"tasks"}
POST /tasks(.:format) {:action=>"create", :controller=>"tasks"}
task GET /tasks/:id(.:format) {:action=>"show", :controller=>"tasks"}
PUT /tasks/:id(.:format) {:action=>"update", :controller=>"tasks"}
DELETE /tasks/:id(.:format) {:action=>"destroy", :controller=>"tasks"}

NESTしたRouteでも絞ろうとすると、

ActionController::Routing::Routes.draw do |map|
map.resources :project, :has_many => :tasks, :except => [:new, :edit]
end
% rake routes
project_tasks GET /project/:project_id/tasks(.:format) {:action=>"index", :controller=>"tasks"}
POST /project/:project_id/tasks(.:format) {:action=>"create", :controller=>"tasks"}
new_project_task GET /project/:project_id/tasks/new(.:format) {:action=>"new", :controller=>"tasks"}
edit_project_task GET /project/:project_id/tasks/:id/edit(.:format) {:action=>"edit", :controller=>"tasks"}
project_task GET /project/:project_id/tasks/:id(.:format) {:action=>"show", :controller=>"tasks"}
PUT /project/:project_id/tasks/:id(.:format) {:action=>"update", :controller=>"tasks"}
DELETE /project/:project_id/tasks/:id(.:format) {:action=>"destroy", :controller=>"tasks"}
project_index GET /project(.:format) {:action=>"index", :controller=>"project"}
POST /project(.:format) {:action=>"create", :controller=>"project"}
project GET /project/:id(.:format) {:action=>"show", :controller=>"project"}
PUT /project/:id(.:format) {:action=>"update", :controller=>"project"}
DELETE /project/:id(.:format) {:action=>"destroy", :controller=>"project"}

projectの方しか絞れない。

こっちの書き方なら両方絞れる。

ActionController::Routing::Routes.draw do |map|
map.resources :projects, :except => [:new, :edit] do |project|
project.resources :tasks, :except => [:new, :edit]
end
end
% rake routes
project_tasks GET /projects/:project_id/tasks(.:format) {:action=>"index", :controller=>"tasks"}
POST /projects/:project_id/tasks(.:format) {:action=>"create", :controller=>"tasks"}
project_task GET /projects/:project_id/tasks/:id(.:format) {:action=>"show", :controller=>"tasks"}
PUT /projects/:project_id/tasks/:id(.:format) {:action=>"update", :controller=>"tasks"}
DELETE /projects/:project_id/tasks/:id(.:format) {:action=>"destroy", :controller=>"tasks"}
projects GET /projects(.:format) {:action=>"index", :controller=>"projects"}
POST /projects(.:format) {:action=>"create", :controller=>"projects"}
project GET /projects/:id(.:format) {:action=>"show", :controller=>"projects"}
PUT /projects/:id(.:format) {:action=>"update", :controller=>"projects"}
DELETE /projects/:id(.:format) {:action=>"destroy", :controller=>"projects"}

スッキリ!

Comments


Option