# Rendering a different view depending on model attributes

**URL:** <https://www.ruby-forum.com/t/rendering-a-different-view-depending-on-model-attributes/103283>\
**Category:** Rails\
**Created:** [May 3, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/rendering-a-different-view-depending-on-model-attributes/103283 "2007-05-03T19:57:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [May 3, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/rendering-a-different-view-depending-on-model-attributes/103283/1 "2007-05-03T19:57:44Z")

</div>

Is there a good way to render a different view based on a models  
attributes? For instance, say there is a model called Form, which  
belongs to an Office. And there are 2 offices, say Pre and Post, each  
one using a different view, or sharing a view. So say we have the views

form/show.pre.rhtml  
form/show.post.rhtml  
form/comments.rhtml

So each office has a specific show view, but share a comments view.  
Rather than code something that chooses which view to use inside each  
action, I’d like that to be done automatically. So say at a later date  
I decide to split the comments view separately for each office, I can  
just add a new view for each office and it picks the correct one.  
Thanks for any help!

Aaron

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [May 3, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/rendering-a-different-view-depending-on-model-attributes/103283/2 "2007-05-03T20:02:52Z")

</div>

Would

if @form.office == “Pre”  
render :template =\> “form/show\_pre”  
else  
render :template =\> “form/show\_post”  
end

not work? Obviously, youd have to edit those view names to fit there.  
[Not  
sure that “form/show.pre” wouldn’t have unexpected consequences.]

RSL

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [May 3, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/rendering-a-different-view-depending-on-model-attributes/103283/3 "2007-05-03T20:28:58Z")

</div>

Russell N. wrote:

> Would
> 
> if @form.office == “Pre”  
> render :template =\> “form/show\_pre”  
> else  
> render :template =\> “form/show\_post”  
> end
> 
> not work? Obviously, youd have to edit those view names to fit there.  
> [Not  
> sure that “form/show.pre” wouldn’t have unexpected consequences.]
> 
> RSL

I could do that, I was just trying to avoid that because I could see  
myself repeating myself a lot.

---

<div class="post-metadata">

**Author:** ![d2kagw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5a626/32.png) [@d2kagw](https://www.ruby-forum.com/u/d2kagw)\
**Post date:** [May 3, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/rendering-a-different-view-depending-on-model-attributes/103283/4 "2007-05-03T20:33:56Z")

</div>

Ah. perhaps write it out into a method and just call the method from the  
appropriate places. I wish you could use after\_filters to do renders.

RSL
