Beginner Layouts and Partials question

Hello - very new to rails and MVC, I'm trying to figure out how
layouts/partials co-exist. My basic layout (application.html.erb) looks
like this:

<body>
  <div id="wrapper">
    <%= render :partial => "main/header" %>
    <div id="content">
      <%= render => :partial => "main/menu" %>
      <%= render => :partial => @partial %>
    </div>
  </div>
</body>

and my MainController is:

class MainController

  def index
    @partial = "main/home"
  end

end

Now, I found that I have to create an index.html.erb file that's
identical to the application.html.erb file, or nothing works (neither
can exist without the other). I'm trying to understand what I'm doing
wrong -- obviously I need some index action in my controller, but I'd
like to just maintain one layout file and nothing else.. I'm obviously
missing something big here, so any clarifications will be great :slight_smile:

right: you need an index-file for your index-action (actions and views
pair up)
wrong: it has not to be identical to the layout-file.

try to put
  <%= yield %>
into your layout instead of
  <%= render => :partial => @partial %>
and change your index to
  Hello it's <%= Time.now %>

in your controller just leave it like this
  def index
  end

now, if you want to render a partial, do so by saying it for example
in your view. if you put
  render :partial => foo
in your controller, then by default
  layout => false
is set.

maybe i'm even mistaken somewhere, but anyhow i hope i could help. my
suggestion: get hold of some rails basics.

Here it goes....

A layout is like the container for the content(.. view) relating to
your actions. When an action is called, the action renders its view.
For example, the person controllers index action will look for app/
views/person/index.html.erb. It also looks for the layout it should
render the view in. You can define the layout to use either in the
controller (layout 'application' in your case) or put in in
application.rb, which will then define a global default layout.

You've got the right idea with the erb you posted. You can render
partials in layouts, but for the action to render it's view in the
layout, you need to put <%= yield %>. The entire contents of the
actions's view will get rendered there.

Your layout, application.html.erb should be residing in the app/views/
layout directory. You should add "layout 'application'" to your
controller, add <%= yield %> to the layout, create views for your
actions and try it out. Your views should render inside your layout.

I hope that gives you some idea of how views and layouts interact.

- John

Those were really helpful posts -- I think I got the general idea, in
which I should specify in the controller which layout it should belong
to, and then in the layout file use <%= yield %> to render these views.
I'll keep playing with it and see what comes up.

Thanks!

sa 125 wrote:

Those were really helpful posts -- I think I got the general idea, in
which I should specify in the controller which layout it should belong
to, and then in the layout file use <%= yield %> to render these views.
I'll keep playing with it and see what comes up.

Thanks!

Take this example:

app/controllers/people_controller.rb:

class PeopleController < ApplicationController
  # layout 'custom'

  # GET /people
  # GET /people.xml
  def index
    @people = Person.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @people }
    end
  end
end

app/views/layouts/application.html.erb:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>My Application</title>
</head>
<body>
  <h1>Header in Application layout</h1>
  <%= yield %>
  <p>Footer in layout</p>
</body>
</html>

app/views/layouts/people.html.erb:

Robert Walker wrote:

layout by first looking in app/layouts/ for a layout template named

Correction app/views/layouts/

You may also want to read the getting started guide (and others) on http://guides.rubyonrails.org

These were all really helpful -- I really appreciate the depth of the
responses. I now only have a problem with using a collection in
rendering a partial. I think I followed the exact rules, but here is
what I did anyway:

The layout (app/views/layouts/application.html.erb):
...
<body>
  <%= yield :title %>
  <br />
  <%= yield %>
</body>
...

the controller (app/controllers/people/people_controller.rb):

class PeopleController < ApplicationController
  layout "application" # default, but specified anyway
  def index
    @people = Person.find(:all)
  end
end

in the view (app/views/people/index.html.erb):

<% content_for :title do -%>
  <h3>Listing People</h3>
<% end -%>

<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <%= render :partial => "people", :collection => @people %>
</table>

in the partial (app/views/people/_people.html.erb):

<tr class=<%= cycle("odd","even")%>>
  <td><%= h person.first_name %></td>
  <td><%= h person.last_name %></td>
</tr>

I realize the partial doesn't recognize the person object, but I thought
rails handled the conversion between the collection @people of the view
to the iteration in the partial. So.. I'm not sure what I might be doing
wrong. Help is very appreciated - thanks!

i don't see an iteration. try

  for person in people do
    <tr class=<%= cycle("odd","even")%>>
      <td><%= h person.first_name %></td>
      <td><%= h person.last_name %></td>
    </tr>
  end

in the view (app/views/people/index.html.erb):

<% content_for :title do -%>
  <h3>Listing People</h3>
<% end -%>

<table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
  </tr>
  <%= render :partial => "people", :collection => @people %>
</table>

in the partial (app/views/people/_people.html.erb):

<tr class=<%= cycle("odd","even")%>>
  <td><%= h person.first_name %></td>
  <td><%= h person.last_name %></td>
</tr>

----

I made sure I had data available for these people (using
script/console), but I got an error rendering this partial:

undefined local variable or method 'person' for #<ActionView::....>

in app/views/people/_people.html.erb

OK, I figured out my mistake - when creating the partial, I called it
"_people.html.erb" instead of "_person.html.erb", so the rails engine
probably didn't know what to look for. Also needed to change the call to
render from <%= render :partial => "people" ....%> to <%= render
:partial => "person"...%>

I would like to know how to override this behavior so that I could call
the partial and the collections however I want. Thanks to everyone who
helped on this post!

sa 125,

This paper (Modular Page Assembly in Rails) helps a lot:

http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/

Cheers, Sazima