# Multiple views for the same action?

**URL:** <https://www.ruby-forum.com/t/multiple-views-for-the-same-action/112826>\
**Category:** Rails\
**Created:** [August 23, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/multiple-views-for-the-same-action/112826 "2007-08-23T16:49:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2007, 4:49pm UTC](https://www.ruby-forum.com/t/multiple-views-for-the-same-action/112826/1 "2007-08-23T16:49:53Z")

</div>

I expect those that have taken the time to read this crazy title  
think, ! Whoa ! what terrible newbie question is this??  
I am writing a generic inventory application that handles a WIDE  
variety of inventory items whose fields can be very different. My  
objective is to write one application and based on the CATEGORY of the  
item, to display the appropriate new, edit, index, show templates  
(obviously showing the pertinent fields). I’ve thought that I could  
have one terribly long .rhtml of each and have a \<% case statement %\>  
to control what rhtml lines are presented to the user. Is this the  
‘smart’ way to approach this?  
Thank you,  
Kathy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/multiple-views-for-the-same-action/112826/2 "2007-08-23T17:17:56Z")

</div>

Hi –

On Thu, 23 Aug 2007, removed\_email\_address@domain.invalid wrote:

> ‘smart’ way to approach this?  
> Thank you,  
> Kathy

My inclination would be to do the branching in the controller, and do  
a ‘render’ command for the right template. And/or: you could have  
multiple partial templates, and set a name in the controller and then  
do:

\<%= render :partial =\> @partial\_name … %\>

in the view.

I’m not totally opposed to if/else (or case) logic in views. I  
sometimes do things like:

\<% if @admin\_session %\>

…show a delete button…

\<% end %\>

But when it gets too long, it’s a sign to me that it’s time to split  
it out into multiple templates, full or partial.

I think it would also be easier to have one or more directories with,  
so to speak, an inventory of inventory forms, rather than have it all  
inlined in one template.

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 23, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/multiple-views-for-the-same-action/112826/3 "2007-08-23T20:09:30Z")

</div>

rather than using \<% if @admin\_session %\> you should look into using a  
helper method with a block such as:

\<% admin\_content do -%\>  
information for admins only  
\<% end %\>

and then in your helper:

def admin\_content(&block)  
yield if session[:user].is\_admin?  
end

that way if you change the way administrator access is handled, you  
don’t have to change every “if @admin\_session” instance.

Adam
