2010-08-08 76 views
2

究竟是什麼?rails [template + partial]導致重複輸出!

在文章/ _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 

在文章/ _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 

在文章/ 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 

在layouts/application.html.erb中(僅限相關部分)

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

結果是下面的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> 

等。 (@post中有15個項目;每個項目應該有一個單行的描述,但正如你所看到的,每個項目都打印出全部15個描述。)

我也試過改變index.html.haml到

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

但引起更瘋狂的行爲 - 中的第一項有一個描述,第二有兩個,等

這是怎麼回事?

回答

2

如果您在給定渲染過程中多次調用content_for助手,它將連接內容而不是覆蓋它,導致您在此處看到的行爲。我建議將post_brief + post_wrapper轉換爲單個局部渲染而不是局部佈局。

+0

啊,我想這是有道理的(如果你需要在content_for中間做一些邏輯,所以你想在兩個地方指定'content_for')。好吧,我會這樣做的。我分裂post_brief和post_wrapper的理由是幹掉show.html.haml在'_post_wrapper'中也使用了相同的東西,但這聽起來像是在這裏引起問題。 – unsorted 2010-08-08 14:03:21

+0

在這種情況下,您可以將這些分解爲助手或分成小部分以供重用。 – 2010-08-09 03:48:36