views:

13

answers:

1

What on earth?

In posts/_post_brief.html.haml:

- title link_to "#{post_brief.title} by #{post_brief.user.full_name}", post_brief

- content_for :info do
    - if post_brief.tags.count > 0
        Tags: #{post_brief.tags.collect {|t| t.name}.join(", ")}

- content_for :post_body do
    =post_brief.message

In posts/_post_wrapper.html.haml:

.post
  .right
    %h2= yield :title
    %p.post-info= yield :info
    = yield :post_body
  .left
    %p.dateinfo
      JAN
      %span 31
    .post-meta
      %h4 Playlist Info
      %ul
        %li.user
          %a{:href => "#"} Erwin
        %li.time
          %a{:href => "#"} 12:30 PM
        %li.comment
          %a{:href => "#"} 2 Comments
        %li.permalink
          %a{:href => "#"} Permalink

In posts/index.html.haml:

- title "Posts"

- content_for :info do
    Lots of posts here!

- content_for :main_content do
    -# @posts.each do |post|
    = render :partial => "post_brief", :layout => "post_wrapper", :collection => @posts

    = link_to 'New post', new_post_path

In layouts/application.html.erb (relevant portion only)

<div id="main">
  <%= yield(:main_content) or yield %>
</div>

The result is the following HTML:

<div class='post'>

  <div class='right'>
    <h2><a href="/posts/545688642">Love Potion No. 23 by John Smith</a></h2>
    <p class='post-info'>Lots of posts here!</p>
    The  is such a flawed product!
    This potion, Love Potion No. 7, is defective!
    This potion, Love Potion No. 25, is defective!
    This potion, Love Potion No. 13, is defective!
    This potion, Love Potion No. 17, is defective!
    This potion, Love Potion No. 3, is defective!
    This potion, Love Potion No. 21, is defective!
    This potion, Love Potion No. 4, is defective!
    This potion, Love Potion No. 10, is defective!
    This potion, Love Potion No. 14, is defective!
    This potion, Love Potion No. 22, is defective!
    This potion, Love Potion No. 8, is defective!
    This potion, Love Potion No. 18, is defective!
    This potion, Love Potion No. 1, is defective!
    This potion, Love Potion No. 23, is defective!
  </div>
  <div class='left'>
    <p class='dateinfo'>
      JAN
      <span>31</span>

    </p>
    <div class='post-meta'>
      <h4>Playlist Info</h4>
      <ul>
        <li class='user'>
          <a href='#'>Erwin</a>
        </li>
        <li class='time'>

          <a href='#'>12:30 PM</a>
        </li>
        <li class='comment'>
          <a href='#'>2 Comments</a>
        </li>
        <li class='permalink'>
          <a href='#'>Permalink</a>

        </li>
      </ul>
    </div>
  </div>
</div>
<div class='post'>
  <div class='right'>
    <h2><a href="/posts/545688642">Love Potion No. 23 by John Smith</a></h2>
    <p class='post-info'>Lots of posts here!</p>

    The  is such a flawed product!
    This potion, Love Potion No. 7, is defective!
    This potion, Love Potion No. 25, is defective!
    This potion, Love Potion No. 13, is defective!
    This potion, Love Potion No. 17, is defective!
    This potion, Love Potion No. 3, is defective!
    This potion, Love Potion No. 21, is defective!
    This potion, Love Potion No. 4, is defective!
    This potion, Love Potion No. 10, is defective!
    This potion, Love Potion No. 14, is defective!
    This potion, Love Potion No. 22, is defective!
    This potion, Love Potion No. 8, is defective!
    This potion, Love Potion No. 18, is defective!
    This potion, Love Potion No. 1, is defective!
    This potion, Love Potion No. 23, is defective!
  </div>
  <div class='left'>
    <p class='dateinfo'>
      JAN
      <span>31</span>
    </p>
    <div class='post-meta'>
      <h4>Playlist Info</h4>

      <ul>
        <li class='user'>
          <a href='#'>Erwin</a>
        </li>
        <li class='time'>
          <a href='#'>12:30 PM</a>
        </li>
        <li class='comment'>

          <a href='#'>2 Comments</a>
        </li>
        <li class='permalink'>
          <a href='#'>Permalink</a>
        </li>
      </ul>
    </div>
  </div>
</div>

and so on. (There are 15 items in @post; each one is supposed to have a one-line description, but as you can see, all 15 descriptions get printed for each item.)

I also tried changing index.html.haml to

- content_for :main_content do
    - @posts.each do |post|
        = render :partial => "post_brief", :layout => "post_wrapper", :locals => {:post => post}

but that caused even crazier behavior -- the first entry had one description, the second had two, etc.

What's up?

+2  A: 

If you call the content_for helper multiple times in the course of a given render, it will concatenate the content rather than overwriting it, leading to the behavior you see here. I'd recommend turning post_brief + post_wrapper into a single partial and just rendering that rather than using a partial layout.

Greg Campbell
Ah, I guess that makes sense (like if you needed to do some logic in the middle of your content_for so you wanted to specify `content_for` in two places). Okay, I'll do that. My reasoning for splitting post_brief and post_wrapper was to DRY up the fact that show.html.haml also used the same stuff in `_post_wrapper`, but it sounds like that's causing problems here.
unsorted
In that case, you could factor these out into helpers or into small separate partials for reuse.
Greg Campbell