How to use Rails caches_action with layout:false and dynamically change meta tags?
<p>I am stuck at what I think is a very simple/common usecase in a Rails web application. I want to use &quot;caches_action, layout:false&quot; and display, from the layout, dynamic tags that will be set by the action (either from the view or the controller).</p>
<p>I could not find any standard rails way to do this as <strong>content_for</strong> does not work with <strong>caches_action</strong>, instance variables are not cached (?), and the metatags helper gems that I have tried (<a href="https://github.com/lassebunk/metamagic/issues/19" rel="nofollow noreferrer">metamagic</a> and <a href="https://github.com/kpumuk/meta-tags" rel="nofollow noreferrer">meta-tags</a>) do not support this usecase.</p>
<p>Is there any way to do this ?</p>
<h1>Example</h1>
<p>I am using caches_action, layout:false on a SandboxController#show method</p>
<pre><code>#app/controllers/sandbox_controller.rb
class SandboxController &lt; ApplicationController

  caches_action :show, layout: false, expires_in: 1.minute

  def show
    @meta_title = &quot;Best page ever&quot;
    do_some_expensive_operation
  end

end
</code></pre>
<p>The view</p>
<pre><code>#app/views/sandbox/show.html.erb
We are in show action.
</code></pre>
<p>The layout</p>
<pre><code>#app/views/layouts/application.html.erb
&lt;title&gt;&lt;%= @meta_title %&gt;&lt;/title&gt;
Debug: &lt;%= @meta_title %&gt; &lt;br/&gt;
&lt;%= yield %&gt;
</code></pre>
<p>Thanks !</p>