Rails: allowing a partial to only be rendered once

前端 未结 1 610
执念已碎
执念已碎 2021-01-22 07:18

Is there a way to code a rails partial such that it can only be rendered once, no matter how many times the containing view attempts to render it?

Why:

相关标签:
1条回答
  • 2021-01-22 07:37

    You could make use of the instance variables. However, you have to ensure that the instance variables is bounded to the views only and will not be crashed.

    Something like:

    # in your view
    <% @widget_partial_rendered_popovers ||= [] %>
    
    <% popover_partials.each do |pp| %>
      <% if !@widget_partial_rendered_popovers.include?(pp) %>
        <% @widget_partial_rendered_popovers << pp %>
        display the popover
      <% end %>
    <% end %>
    

    If you are able to collect the popover things early in the controller, it would be cleaner to just render all the popovers in a place. As I don't know what's your exact structure is, I could just give you what you have required.

    0 讨论(0)
提交回复
热议问题