Aniket Tiwari
Aniket Tiwari

Reputation: 3998

Render a partial only once

Is there any way to render a partial only once. What I am trying to say here is I have a partial

_popup_message.html.erb. I am rendering this partial from multiple places on a same page. The problem is inside the partial I have few id attributes. So, if the partial is getting rendered multiple times on a page so HTML is giving error that id attribute is used twice or thrice. So, is there any way in rails by which I can check whether that partial already rendered if yes then don't render it.

Here is my code

<div class="row">
  <div class="medium-centered columns"> 
    <div class="popup-message">  
        <%= dashboard_tile_message.html_safe %>  
          ###so if the below partial got rendered once I don't want to render it 
          <%= render :partial => 'popup_message', locals: { user: current_user, setting: settings } %>
    </div>
  </div>
</div>

Upvotes: 1

Views: 625

Answers (1)

Patience
Patience

Reputation: 11

If someone got struggled, here the example what @max want to propose(haml):

- @rendered ||= false
- unless @rendered 
  #- Your partial code
- @rendered = true```

Upvotes: 1

Related Questions