Peter Michaux
2006-Jul-17 01:05 UTC
If controllers inherit then why not views? / DRY, REST & other buzzwords.
Hi, If I have controllers that inherit actions and helper methods then why not views that "inherit" templates and partials? I''ve been trying to DRY up my code and make it more REST friendly after the fallout of DHH''s "A World of Resources" slides and blog post. With a move towards more RESTful controller design, we are going to have more controllers with the basic 7 actions (index, show, edit, update, new, create, destroy). Much of the functionality of these actions can be moved into ApplicationController to keep our code DRY. This is a move away from scaffold-type controllers and is shown on http://www.artofmission.com/articles/2006/07/05/rest-ful-technique-make-your-controllers-even-smaller It would be great if views could be shared in the same way instances inherit from superclasses. Following the art of mission example, when rendering the page create or edit pages == views/application/new.rhtml = <h1>Create a new record</h1> <%= render :partial=>''form'' %> == views/application/edit.rhtml = <h1>Edit a new record</h1> <%= render :partial=>''form'' %> == views/page/edit.rhtml (overrides views/application/edit.rhtml) = <h1>Edit a new PAGE</h1> <%= render :partial=>''form'' %> == views/page/_form.rhtml = <%= form_tag %> ... <%= end_form_tag %> So when page/new is being rendered views/application/new.rhtml and views/page/_form.rhtml are used since Rails can''t find a template called views/page/new.rhtml. When page/edit is being rendered view/page/edit.rhtml and views/page/_from.rhtml are used. So Rails would always starts looking for templates in the view directory corresponding to the controller and keep looking up view directories in a search path that match the controllers inheritance path. For each partial the process would be just the same. I tried to keep this short. All I''m trying to say is make view directories work like classes and make templates work like instance methods. Sound like it would be nice and DRY to effectively share views? Thanks, Peter