DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Lance has posted 24 posts at DZone. View Full User Profile

DRY Polymorphic Routes In Rails 2.3.8

  • submit to reddit
        // Say you have a Post base class and BlogPost and WikiPost subclasses.  And you want to map them all to a single controller so you can use 1 set of views.  Do this in your routes:

map.resources :posts, :controller => :posts, :requirements => {:type => "post"}
map.resources :blog_posts, :controller => :posts, :requirements => {:type => "blog_post"}
map.resources :wiki_posts, :controller => :posts, :requirements => {:type => "wiki_posts"}

// Then in the PostsController, you can get the type with