Rails在循环中呈现局部

时间:2022-10-08 13:17:25

I am new to rails and I am trying to render a partial within a loop as such.

我是rails的新手,我正在尝试在循环中呈现部分。

Here , books is an array eager loaded in controller.

在这里,books是一个在控制器中热切加载的数组。

 books.each do |book|
   <%= render 'books/book', :book => book %>
 end

This works fine. But when the books array is really huge, it takes time for the view to load completely. Is there any other efficient way using which the same can be achieved?. I tried partial with collection(like here) as well, but even that did not make much difference in the load time of books. Any suggestions would be highly appreciated. Thanks.

这是很好。但是当books数组非常大时,视图完全加载需要时间。有没有其他有效的方法可以达到同样的效果?我也尝试了部分收藏(比如这里),但即使这样也不会对图书的装载时间产生太大影响。如有任何建议,我们将不胜感激。谢谢。

4 个解决方案

#1


3  

Rendering partial in a loop consume too much time because of open/close partial file every iteration. Instead of partial try to use your own helper for this purpose.

由于每次迭代都打开/关闭部分文件,在循环中呈现部分文件会花费太多时间。不要为了这个目的使用你自己的助手。

#2


1  

If you have list of values to display in the same view then you can iterate the values in the same view instead of rendering a new partial each time. If it's not the case then it is better to pass values from controller to your view. Hope it helps.

如果您有一个值列表要在同一个视图中显示,那么您可以在同一个视图中迭代这些值,而不是每次都呈现一个新的部分。如果不是这样,那么最好将值从控制器传递到视图。希望它可以帮助。

#3


0  

You write that you're new to ruby/rails, so have you ever tried using pagination to solve your performance-problem?

您编写的是ruby/rails的新内容,您是否曾尝试使用分页来解决性能问题?

A pagination will split your list into parts of e.g. 20 books and sets a paginator mostly at the bottom of your table.

分页会将你的列表分成20本书的部分,并在你的表格底部设置一个分页器。

#4


0  

I recently worked on a project in which I needed to render a table with about 1000 rows and I obviously experienced performance issues.

我最近参与了一个项目,在这个项目中,我需要呈现一个包含大约1000行的表,显然我遇到了性能问题。

When pagination cannot be applied (due to requirements) and speed is required, then helpers is the solution (as already answered by Dima Melnik).

当无法应用分页(由于需求)和速度时,helper是解决方案(Dima Melnik已经回答过了)。

To prove what i said, i give a link to a performance test produced by Ben Scofield: http://viget.com/extend/helpers-vs-partials-a-performance-question

为了证明我所说的,我提供了本·斯科菲尔德制作的性能测试链接:http://viget.com/extend/helpers-vs-partials-a-performance-question

#1


3  

Rendering partial in a loop consume too much time because of open/close partial file every iteration. Instead of partial try to use your own helper for this purpose.

由于每次迭代都打开/关闭部分文件,在循环中呈现部分文件会花费太多时间。不要为了这个目的使用你自己的助手。

#2


1  

If you have list of values to display in the same view then you can iterate the values in the same view instead of rendering a new partial each time. If it's not the case then it is better to pass values from controller to your view. Hope it helps.

如果您有一个值列表要在同一个视图中显示,那么您可以在同一个视图中迭代这些值,而不是每次都呈现一个新的部分。如果不是这样,那么最好将值从控制器传递到视图。希望它可以帮助。

#3


0  

You write that you're new to ruby/rails, so have you ever tried using pagination to solve your performance-problem?

您编写的是ruby/rails的新内容,您是否曾尝试使用分页来解决性能问题?

A pagination will split your list into parts of e.g. 20 books and sets a paginator mostly at the bottom of your table.

分页会将你的列表分成20本书的部分,并在你的表格底部设置一个分页器。

#4


0  

I recently worked on a project in which I needed to render a table with about 1000 rows and I obviously experienced performance issues.

我最近参与了一个项目,在这个项目中,我需要呈现一个包含大约1000行的表,显然我遇到了性能问题。

When pagination cannot be applied (due to requirements) and speed is required, then helpers is the solution (as already answered by Dima Melnik).

当无法应用分页(由于需求)和速度时,helper是解决方案(Dima Melnik已经回答过了)。

To prove what i said, i give a link to a performance test produced by Ben Scofield: http://viget.com/extend/helpers-vs-partials-a-performance-question

为了证明我所说的,我提供了本·斯科菲尔德制作的性能测试链接:http://viget.com/extend/helpers-vs-partials-a-performance-question