2010-08-08 4 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 

레이아웃/application.html.erb (관련 부분에만 해당)

그래서

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

과 :

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

결과는 다음 HTML입니다. (@ 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

+0

그럴 경우, 이들을 헬퍼 나 재사용을위한 작은 별도의 부분으로 분해 할 수 있습니다. –