Understanding Common Views

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

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 :wink:

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

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 :slight_smile:

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.

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.

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

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… :slight_smile:

Cheers
Mohit.

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! :slight_smile:

Cheers
Mohit.