Home > other >  gsub in cached partial
gsub in cached partial

Time:04-09

I have a cached partial in app/views/people:

<% cache key do %>
    <tr>
      <td>
        <%= dynamic_value %>

Is there a way, inside the view itself to do something like:

<% temp = cache key do %>
    <tr>
      <td>
        <%= dynamic_value %>

...

<%= temp.gsub("Smith","Jones") %>

The idea is to encache a slightly expensive dynamic render with placeholder text, then gsub it with dynamic values inside the partial.

Is this possible?

Thanks, Kevin

CodePudding user response:

capture helper is used for grabbing parts of the template https://api.rubyonrails.org/v7.0.2.3/classes/ActionView/Helpers/CaptureHelper.html#method-i-capture

If there is no way to only cache the expensive bit and then add user name. Any part of the template can be rendered and captured into a variable, if you modify it later it needs to be marked as html safe.

<% temp = capture do %>
  <% cache key do %>
    <%= dynamic_value %>
  <% end %>
<% end %>

<%= temp.gsub("Smith","Jones").html_safe %>
  • Related