我有rails操作,其中包括以下HAML部分:
- @items.each_with_index do |item, i|
- item_no = 'item' + item.item_no.to_s
.item
= form_for item, remote: true, html: { multipart: true, autocomplete: 'off' } do |f|
= f.hidden_field :parent_id, :value => @parent.id
= f.hidden_field :image, value: item.image
...
...
// 5-6 attributes
- if @parent.has_item_numbers?
.serial-number{ class: ('right' if item.item_no.odd?) }
= item.item_no与其items相关联的parent的数量可能会有很大的差异,根据用户的不同也可以达到2000-3000。我们还没有限制父母的子女数量。在我决定一个limit:之前,是否有一种方法可以加速对.each_with_index方法的渲染,因为现在需要10秒才能完成对本地的请求。
对于速度,有一些关于将部分转换为方法/块的讨论,但我不完全理解。
另外,值得注意的是,同样的部分在rails 5.0.4中的表现要好得多,但是它在rails 5.1.4中的表现已经大大减缓了。
发布于 2017-09-25 15:14:05
一般不应该在视图中迭代。相反,您应该使用集合渲染,它将为您执行迭代,并且是以速度快而闻名,并且允许您实现片段缓存:
你的观点:
= render @items, parent: @parent你的部分(即_item.html.haml):
- item_no = 'item' + item.item_no.to_s
.item
= form_for item, remote: true, html: { multipart: true, autocomplete: 'off' } do |f|
= f.hidden_field :parent_id, :value => @parent.id
= f.hidden_field :image, value: item.image
...
...
// 5-6 attributes
- if parent.has_item_numbers?
.serial-number{ class: ('right' if item.item_no.odd?) }
= item.item_nohttps://stackoverflow.com/questions/46408458
复制相似问题