zargony.com

Struggling through Ruby on Rails and Linux

Archives for: May 2008

Sharing controllers and views with polymorphic resources

Posted by Andreas on Friday, May 16, 2008 at 17:26 (CEST)

A while ago I faced a problem with controllers for nested resources I wanted to reuse in different contexts (polymorphic resources). For example, I had a simple Todo model that is used to store tasks that have to be done. Having multiple users which are organized in teams, the goal was to have user-specific (personal) todos and team-specific todos. Obviously, both kind of todos could be handled exactly the same way (same controller, same views) – except that the context differs.

So here’s a way to use a single controller for a polymorphic resource.

Read more »
© 2009 by Andreas Neuhaus, Dortmund, Germany • Login • Style based on Scribbish • Powered by Ruby on Rails