views:

751

answers:

2

I have a Rails site using STI with the following classes:

Pages

Homepage < Pages

LandingPage < Pages

On the front-end all requests get handled by the Pages controller. However, if the object detected is actually an instance of LandingPage, i'd like to have the action on a LandingPages controller get called. (for example, the show method in the child controller classes will pull in some specific lookups that aren't always relevant).

Any suggestions on how to best accomplish this?

Thanks

+1  A: 

This sounds a bit like you are clouding the MVC distinction, but it should be doable.

I'd add a series of tests on the Pages model (e.g. supports_buzzbar_foo? or wiggums_itemization_controller, then override them as appropriate in the subclasses) and use these in the view to conditionally generate the appropriate links to the controller methods you want.

That way you're keeping each part (roughly) doing it's job.

MarkusQ
A: 

Markus' solution should work. You could also keep your links in the views pointed to Pages, evaluate the incoming object and then redirect_to the appropriate controller based on the object class.

However, unless you're performing completely different actions with each type of object, then you'll wind up with duplicate code in your controllers. So you might be better off sticking with the Pages controller and just adding some methods that handle the extra lookups that are needed for that object.

insane.dreamer