Faster Rails single partial render

时间:2019-04-16 23:41:30

标签: ruby-on-rails caching haml partial

I need to avoid an often repeat of the same code block into template. Example:

%a{href:item_path(item.web_title,@relative_link_options)}
  =item.show_field_locale('title')
  -unless item.get_image_path('has_image',"item_#{item.id}.png").blank?
    =image_tag(item.get_image_path('has_image',"item_#{item.id}.png"))

So, I describe many items, NOT from collection @items. That are has-one relations or small has-many relations. I have a lot of such pages, so too many different content for caching.

To display every item I need to repeat an above code block. Also I could use partial _item.html.haml and use it via render partial: 'item' But it slows an application (time to prepare Views in ms). How could I avoid it?

1 个答案:

答案 0 :(得分:1)

例如,我正在使用https://github.com/igorkasyanchuk/embed_view

解决此类问题

但是它需要有ERB视图