# Understanding Common Views

**URL:** <https://www.ruby-forum.com/t/understanding-common-views/64409>\
**Category:** Rails\
**Created:** [June 25, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409 "2006-06-25T15:42:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/1 "2006-06-25T15:42:02Z")

</div>

Hi Everyone,

I’m trying to formulate my thoughts on how views work in RoR and I  
understand the basic bits -

1. Every controller has its own view defined using rhtml files in the  
views/[controller\_name] folder.
2. There is one “view” file (rhtml) for every in the controller and has  
the same name as the method.
3. For portions that are common, partials can be used - file names begin  
with “\_”

This bit is clear enough to me (though do correct me if something is  
wrong in the above statements). Now, I am wondering about how to do  
some of the other things that have been asked in a couple of emails  
(though I’m slightly confused). Some of the early questions are as  
follows:

1. Which directory/ file should I use to create the HTML that is common  
to the whole site. I can create the general layout of the page using  
something like NetObjects Fusion (which I use for static sites) and I’m  
guessing that I can modify the generated output to include the embedded  
ruby statements.
2. How do we render output from multiple controllers onto the same  
page? For example, the 5 most recent news headlines as well as the 5  
most recent events, etc.
3. I’ve seen a bit about putting things into “shared/partials” and  
“views/layouts” and I’m not sure what belongs where :-S

I think my confusion is between “views” and “layouts”… but I just got  
hold of the AWDR book (PDF) and also the main article that I found that  
explains some of this [1] and hopefully will be able to understand some  
of this a bit better. So, I am reading on but I hope some of you can  
explain these concepts as you are applying them.

Anything else that comes to mind at this stage?

Thanks,  
Mohit.

[1] [http://www.nabble.com/Common-menu-t801066.html#a2084679](http://www.nabble.com/Common-menu-t801066.html#a2084679)

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/2 "2006-06-25T16:04:02Z")

</div>

Hi Mohit,

Mohit S. wrote:

> 1. Which directory/ file should I use to create the HTML that is common to  
> the whole site

/views/layouts/application.rhtml

> 1. How do we render output from multiple controllers onto the same page?

In general, you’re going to be looking at Ajax for something like this,  
probably using RJS.

> 1. I’ve seen a bit about putting things into “shared/partials” and  
> “views/layouts” and I’m not sure what belongs where :-S

Depends on what ‘what’ you’re talking about 😉

> I think my confusion is between “views” and “layouts”…

The way I conceptualize it is: layouts ‘contain’ views which may  
‘contain’  
partials. YMMV.

> but I just got hold of the AWDR book (PDF)

That will definitely help. Also important: working through tutorials,  
actually writing code from scratch - especially small applets to  
understand  
how pieces of RoR work, learning to use irb and the console, the Pickaxe  
book, …

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/3 "2006-06-25T16:13:23Z")

</div>

Bill W. wrote:

> understand how pieces of RoR work, learning to use irb and the  
> console, the Pickaxe book, …
> 
> hth,  
> Bill

Thanks, Bill - gets me started. I particularly like your statement  
about “layouts ‘contain’ views which may ‘contain’ partials” in spite of  
the “YMMV” disclaimer 🙂

As for the confusion between “shared/partials” and “views/layouts”, can  
someone give me an example of using a shared partial? I’d imagine that  
a view would be quite specific to the controller specially if the “site”  
level layout is going into views/layouts/application.rhtml

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/4 "2006-06-25T16:32:10Z")

</div>

Hi Mohit,

Concerning your question about layouts and partials, I’ll try to  
explain through my experience.

Layouts are most useful when you put up all your page together. A  
typical layout will include a header, navigation component and a  
footer. As you might be aware most pages share the same header and  
footer, but NOT all of them. You may have a front site with heavy  
graphics but some sort of members area merely the same look and feel  
but different layout. So wouldn’t it be wise to compartmentize your  
layout blocks? That’s were the shared and partial views come to help.  
You place frequent view parts in partials and include them in your  
views. For instance in this case, your layout may include one partial  
render for header, one partial render for footer.

This is not the only way to make use of partials. For instance,  
consider a table listing orders. you may want to reuse this table in  
many views. For example you may have a page where you enable  
searching orders and listing them. Or you may have a page where it  
just lists the recent orders. That means separate controllers both  
returning some sort of @orders to the view. So why rewrite the same  
table in views. Just create one partial such as \_order\_list.rhtml and  
use that in your views. Another fancy but very helpful case might be

- actually I do use it a lot - you may create a partial for lines of  
tables. Then you might form up your table using partial as the – element for your table.

Hope I could have explained.

Regards,  
Oyku.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/5 "2006-06-25T17:57:51Z")

</div>

On Jun 25, 2006, at 9:10 AM, Mohit S. wrote:

> As for the confusion between “shared/partials” and “views/layouts”,  
> can someone give me an example of using a shared partial? I’d  
> imagine that a view would be quite specific to the controller  
> specially if the “site” level layout is going into views/layouts/  
> application.rhtml

I’ll give you another example along with Oyku’s already excellent  
example. I’m just learning Ruby and Rails myself and have found that  
using a “form” partial saves a lot of code space when I have two  
different actions that need the same code. For example, an add and  
edit action. Both need the exact same form, just in different  
contexts. And that way when you need to make a change, no matter how  
big or small, you just change that one partial.

I believe I learned that from the Depot app in AWDR, so you should  
hit that soon if you haven’t already.

-geoffrey

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/6 "2006-06-25T18:04:03Z")

</div>

Geoffrey L. wrote:

> using a “form” partial saves a lot of code space when I have two  
> different actions that need the same code. For example, an add and  
> edit action. Both need the exact same form, just in different  
> contexts. And that way when you need to make a change, no matter how  
> big or small, you just change that one partial.
> 
> I believe I learned that from the Depot app in AWDR, so you should hit  
> that soon if you haven’t already.
> 
> -geoffrey  
> Thanks Geoffrey,

I do understand partials and their use - that is not a problem at all!  
My problem is when going beyond the views for a particular controller to  
more “global” issues such as the application level design or having  
common elements that should be there to give the site a consistent look,  
common menus, etc. I’m right now playing with the options offered by  
layouts to see what else can be added in there… 🙂

Cheers  
Mohit.

---

<div class="post-metadata">

**Author:** ![mohits](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbe5ce/32.png) [@mohits](https://www.ruby-forum.com/u/mohits)\
**Post date:** [June 25, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/understanding-common-views/64409/7 "2006-06-25T17:20:36Z")

</div>

Oyku Gencay wrote:

> but different layout. So wouldn’t it be wise to compartmentize your  
> sort of @orders to the view. So why rewrite the same table in views.  
> Just create one partial such as \_order\_list.rhtml and use that in your  
> views. Another fancy but very helpful case might be - actually I do  
> use it a lot - you may create a partial for lines of tables. Then you  
> might form up your table using partial as the
> 
> – element for  
> your table.
> 
> Hope I could have explained.
> 
> Regards,  
> Oyku.  
> Hi Oyku,

Thanks for the detailed reply - I think I understand what you’re  
saying… now, it is time to try it in some implementations and come  
back to the more experienced when I get stuck! 🙂

Cheers  
Mohit.
