简体   繁体   English

在Ruby on rails上渲染部分集合正在增加项目

[英]Render partial in Ruby on rails a collection is multiplying items

I want to display a list of items in a page in Ruby-on-Rails. 我想在Ruby-on-Rails中显示页面中的项目列表。 I use partials 我使用偏爱

in my index.html.erb file I have: 在我的index.html.erb文件中我有:

<%= @lista = News.find(:all, :order => Document::COL_DATE + ' DESC, id DESC')
    render :partial => "newsitem",
           :layout => "list_news",
           :spacer_template => "spacer",
           :collection => @lista
%>

in _list_news.html.erb I have: _list_news.html.erb我有:

<div class="news">
  <%= yield %>
</div>

in _spacer.html.erb I have <hr/> _spacer.html.erb我有<hr/>

in _newsitem.html.erb I have _newsitem.html.erb我有

<%= newsitem_counter + 1 %>
<!-- Code to print details for one item -->

The problem is that it prints the list multiple times: 问题是它多次打印列表:

If the list has 3 items, it shows them 3 times: 1,2,3,1,2,3,1,2,3. 如果列表有3个项目,则显示3次:1,2,3,1,2,3,1,2,3。
If it has 7 items, those items are printed 7 times. 如果它有7个项目,那么这些项目将被打印7次。

What is wrong in my code? 我的代码有什么问题?

The :layout option is usually used with :action or a single :partial, not with :collection's. :layout选项通常用于:action或one:partial,而不是:collection's。 The problem: yield is being called for every item in the list. 问题是:为列表中的每个项目调用yield。

You'd have to look at the sources to figure out why :layout and :collection are acting this way; 您必须查看源代码以找出原因:布局和:集合以这种方式运行; but suffice it to say your code should probably just be rewritten so that it doesn't rely on :layout and :collection working together. 但足以说你的代码应该只是被重写,以便它不依赖于:layout和:collection一起工作。

Here's one way you could do so, with the assumption that reusing this code in other views is a high priority. 这是您可以这样做的一种方式,假设在其他视图中重用此代码是一个高优先级。 Unless you're using lots of caching, rendering each partial tends to be fairly slow, especially if your news_feed has many items, so I've consolidated it into one. 除非你使用大量缓存,否则渲染每个部分往往相当慢,特别是如果你的news_feed有很多项,所以我把它合并为一个。

controller/news_controller.rb

class NewsController < ApplicationController
   def index
     @news_feed = News.find(:all, 
                            :order => Document::COL_DATE + ' DESC, id DESC')
   end
end

views/news/index.html.erb

<%= render :partial => "news_feed",
       :locals => { :news_feed => @news_feed} %>

views/news/_news_feed.html.erb

<ul class="news">
  <% news_feed.each_with_index do |news_item, news_item_counter| %>
     <li>
       <%= newsitem_counter + 1 %>
      <%# Code to print details for one item %>
     </li>
  <% end %>
</ul>

If rendering a whole bunch of partials is okay for you running-time wise, you might find this implementation of views/news/_news_feed.html.erb nicer: 如果渲染一大堆部分内容对于运行时明智而言是正常的,您可能会发现views/news/_news_feed.html.erb这种实现更好:

<div class="news">
  <%= render :partial => 'news_item', :collection => news_feed, :spacer_template => "horizontal_break" %> 
</div>

views/news/_news_item.html.erb

<%= newsitem_counter + 1 %>
<%# Code to print details for one item %>

views/news/_horizontal_break.html.erb

<hr />

So instead of rendering :layout, you render one big partial which wraps the collection. 因此,不是渲染:layout,而是渲染一个包裹集合的大部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM