Skip to content

Instantly share code, notes, and snippets.

@ohammersmith
Created April 14, 2009 23:41
Show Gist options
  • Save ohammersmith/95497 to your computer and use it in GitHub Desktop.
Save ohammersmith/95497 to your computer and use it in GitHub Desktop.
Working out nested resources with a self-referential association
(in /Users/otto/Desktop/Incubator/shallow_routes)
communities GET /communities(.:format) {:action=>"index", :controller=>"communities"}
POST /communities(.:format) {:action=>"create", :controller=>"communities"}
new_community GET /communities/new(.:format) {:action=>"new", :controller=>"communities"}
edit_community GET /communities/:id/edit(.:format) {:action=>"edit", :controller=>"communities"}
community GET /communities/:id(.:format) {:action=>"show", :controller=>"communities"}
PUT /communities/:id(.:format) {:action=>"update", :controller=>"communities"}
DELETE /communities/:id(.:format) {:action=>"destroy", :controller=>"communities"}
community_forums GET /communities/:community_id/forums(.:format) {:action=>"index", :controller=>"forums"}
POST /communities/:community_id/forums(.:format) {:action=>"create", :controller=>"forums"}
new_community_forum GET /communities/:community_id/forums/new(.:format) {:action=>"new", :controller=>"forums"}
edit_forum GET /forums/:id/edit(.:format) {:action=>"edit", :controller=>"forums"}
forum GET /forums/:id(.:format) {:action=>"show", :controller=>"forums"}
PUT /forums/:id(.:format) {:action=>"update", :controller=>"forums"}
DELETE /forums/:id(.:format) {:action=>"destroy", :controller=>"forums"}
forum_topics GET /forums/:forum_id/topics(.:format) {:action=>"index", :controller=>"posts"}
POST /forums/:forum_id/topics(.:format) {:action=>"create", :controller=>"posts"}
new_forum_topic GET /forums/:forum_id/topics/new(.:format) {:action=>"new", :controller=>"posts"}
edit_topic GET /topics/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
topic GET /topics/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /topics/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /topics/:id(.:format) {:action=>"destroy", :controller=>"posts"}
topic_posts GET /topics/:topic_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /topics/:topic_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_topic_post GET /topics/:topic_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
# This is where the meat is, and where I've been trying to get.
# Communities have forums have topics have posts, but topics are really posts but without a parent_id
# Your controllers have to know how to deal with the different attributes they may get, but most of that
# logic will get pushed into the model, anyway. It's not super-complex stuff.
map.resources :communities, :shallow => true do |community|
community.resources :forums do |forum|
forum.resources :topics, :controller => :posts do |topic|
topic.resources :posts
end
end
end
(in /Users/otto/Desktop/Incubator/shallow_routes)
edit_community /communities/:community_permalink-:id/edit {:action=>"edit", :controller=>"communities"}
community /communties/:community_permalink-:id {:action=>"show", :controller=>"communities"}
edit_forum /forums/:forum_permalink-:id/edit {:action=>"edit", :controller=>"forums"}
forum /forums/:forum_permalink-:id {:action=>"show", :controller=>"forum"}
edit_post /posts/:forum_permalink-:id/edit {:action=>"edit", :controller=>"posts"}
post /posts/:post_permalink-:id {:action=>"show", :controller=>"posts"}
communities GET /communities(.:format) {:action=>"index", :controller=>"communities"}
POST /communities(.:format) {:action=>"create", :controller=>"communities"}
new_community GET /communities/new(.:format) {:action=>"new", :controller=>"communities"}
GET /communities/:id/edit(.:format) {:action=>"edit", :controller=>"communities"}
GET /communities/:id(.:format) {:action=>"show", :controller=>"communities"}
PUT /communities/:id(.:format) {:action=>"update", :controller=>"communities"}
DELETE /communities/:id(.:format) {:action=>"destroy", :controller=>"communities"}
community_forums GET /communities/:community_permalink-:community_id/forums(.:format) {:action=>"index", :controller=>"forums"}
POST /communities/:community_permalink-:community_id/forums(.:format) {:action=>"create", :controller=>"forums"}
new_community_forum GET /communities/:community_permalink-:community_id/forums/new(.:format) {:action=>"new", :controller=>"forums"}
GET /forums/:id/edit(.:format) {:action=>"edit", :controller=>"forums"}
GET /forums/:id(.:format) {:action=>"show", :controller=>"forums"}
PUT /forums/:id(.:format) {:action=>"update", :controller=>"forums"}
DELETE /forums/:id(.:format) {:action=>"destroy", :controller=>"forums"}
forum_topics GET /forums/:forum_permalink-:forum_id/topics(.:format) {:action=>"index", :controller=>"posts"}
POST /forums/:forum_permalink-:forum_id/topics(.:format) {:action=>"create", :controller=>"posts"}
new_forum_topic GET /forums/:forum_permalink-:forum_id/topics/new(.:format) {:action=>"new", :controller=>"posts"}
edit_topic GET /topics/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
topic GET /topics/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /topics/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /topics/:id(.:format) {:action=>"destroy", :controller=>"posts"}
topic_posts GET /topics/:topic_permalink-:topic_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /topics/:topic_permalink-:topic_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_topic_post GET /topics/:topic_permalink-:topic_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
# total hack, but it actually seems to generate the right routes. Definitely something I'd want to see pushed into the core.
ActionController::Routing::Routes.draw do |map|
map.edit_community "/communities/:community_permalink-:id/edit", :controller => "communities", :action => 'edit'
map.community "/communties/:community_permalink-:id", {:action=>"show", :controller=>"communities"}
map.edit_forum "/forums/:forum_permalink-:id/edit", :controller => "forums", :action => 'edit'
map.forum "/forums/:forum_permalink-:id", {:action=>"show", :controller=>"forum"}
map.edit_post "/posts/:forum_permalink-:id/edit", :controller => "posts", :action => 'edit'
map.post "/posts/:post_permalink-:id", {:action=>"show", :controller=>"posts"}
map.resources :communities, :shallow => true do |community|
community.resources :forums, :path_prefix => "/communities/:community_permalink-:community_id" do |forum|
forum.resources :topics, :controller => :posts, :path_prefix => "/forums/:forum_permalink-:forum_id" do |topic|
topic.resources :posts, :path_prefix => "/topics/:topic_permalink-:topic_id"
end
end
end
end
$ rake routes
(in /Users/otto/Desktop/Incubator/shallow_routes)
blogs GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
blog GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
blog_posts GET /blogs/:blog_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /blogs/:blog_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_blog_post GET /blogs/:blog_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_blog_post GET /blogs/:blog_id/posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
blog_post GET /blogs/:blog_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /blogs/:blog_id/posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /blogs/:blog_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
blog_post_comments GET /blogs/:blog_id/posts/:post_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /blogs/:blog_id/posts/:post_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_blog_post_comment GET /blogs/:blog_id/posts/:post_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_blog_post_comment GET /blogs/:blog_id/posts/:post_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
blog_post_comment GET /blogs/:blog_id/posts/:post_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /blogs/:blog_id/posts/:post_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /blogs/:blog_id/posts/:post_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
ActionController::Routing::Routes.draw do |map|
map.resources :blogs do |blog|
blog.resources :posts do |post|
post.resources :comments
end
end
end
$ rake routes
(in /Users/otto/Desktop/Incubator/shallow_routes)
blogs GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
blog GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
POST /posts(.:format) {:action=>"create", :controller=>"posts"}
new_post GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
comments GET /comments(.:format) {:action=>"index", :controller=>"comments"}
POST /comments(.:format) {:action=>"create", :controller=>"comments"}
new_comment GET /comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_comment GET /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
ActionController::Routing::Routes.draw do |map|
map.resources :blogs
map.resources :posts
map.resources :comments
end
$ rake routes
(in /Users/otto/Desktop/Incubator/shallow_routes)
blogs GET /blogs(.:format) {:action=>"index", :controller=>"blogs"}
POST /blogs(.:format) {:action=>"create", :controller=>"blogs"}
new_blog GET /blogs/new(.:format) {:action=>"new", :controller=>"blogs"}
edit_blog GET /blogs/:id/edit(.:format) {:action=>"edit", :controller=>"blogs"}
blog GET /blogs/:id(.:format) {:action=>"show", :controller=>"blogs"}
PUT /blogs/:id(.:format) {:action=>"update", :controller=>"blogs"}
DELETE /blogs/:id(.:format) {:action=>"destroy", :controller=>"blogs"}
blog_posts GET /blogs/:blog_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /blogs/:blog_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_blog_post GET /blogs/:blog_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
post_comments GET /posts/:post_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /posts/:post_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_post_comment GET /posts/:post_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_comment GET /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
ActionController::Routing::Routes.draw do |map|
map.resources :blogs, :shallow => true do |blog|
blog.resources :posts do |post|
post.resources :comments
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment