代码之家  ›  专栏  ›  技术社区  ›  unsorted

Rails[模板+部分]导致重复输出!

  •  2
  • unsorted  · 技术社区  · 14 年前

    究竟是什么?

    在posts/\u post\u 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
    

    在posts/\u 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
    

    在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
    

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

    但这导致了更疯狂的行为——第一个条目有一个描述,第二个条目有两个描述,等等。

    怎么了?

    1 回复  |  直到 14 年前
        1
  •  2
  •   Greg Campbell    14 年前

    如果你呼叫 content_for helper在给定渲染过程中多次连接内容,而不是覆盖内容,从而导致您在此处看到的行为。我建议将post-brief+post-wrapper转换为一个单独的部分,并只渲染它,而不是使用部分布局。