# MVC .... Many views and one controller

**URL:** <https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225>\
**Category:** Rails\
**Created:** [January 18, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225 "2010-01-18T15:24:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 3:24pm UTC](https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225/1 "2010-01-18T15:24:51Z")

</div>

What’s the right RoR way if a controller needs to display one of many  
views in, say, an index method?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225/2 "2010-01-18T15:32:30Z")

</div>

Ralph S. wrote:

> What’s the right RoR way if a controller needs to display one of many  
> views in, say, an index method?

You can use render or redirect\_to. But why would an index method need  
this?

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 3:36pm UTC](https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225/3 "2010-01-18T15:36:00Z")

</div>

2010/1/18 Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> What’s the right RoR way if a controller needs to display one of many  
> views in, say, an index method?

Do you mean you want several different views all of which are ‘index’  
views but arranged differently? If so then one method is to pass  
parameters to the index action via the url to determine which data and  
view to show.

Colin

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 4:29pm UTC](https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225/4 "2010-01-18T16:29:16Z")

</div>

For each controller in the line,  
it’s defined methods be actions  
for the implicit render of the view  
with the names that match def and view,  
If you write this way, you’ll save some typing  
If you practice everyday, you may find a better way.  
If you want to write about a Resource, it helps  
to think about and read into REST.  
It makes most sense when you have a noun.

non-rest Controller  
BakeriesController  
host:port/bakeries/index  
host:port/bakeries/by\_city  
host:port/bakeries/that\_have\_nice\_scones

REST resource PicklesController  
host:port/pickles/index/  
host:port/pickles/show/1

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 11:15pm UTC](https://www.ruby-forum.com/t/mvc-many-views-and-one-controller/197225/5 "2010-01-18T23:15:32Z")

</div>

I would second Colin’s suggestion.

Basically, you use some clue (URL parameter, session, database state  
or any external data) to decide what to render at the moment. If I  
needed such a thing, my approach would be to have several index  
partials or templates that I would render selectively. The naming  
would be \_index\_standard.xyz, \_index\_specific\_1.xyz or something along  
these lines. Obviously, I wouldn’t put the selection logic in the view  
itself.

- Aleksey
