I'm trying to insert content into the head of my page on an per element basis, so I'd like to be able to specify something like this in a partial:
# _partial.html.erb
<%= content_for :style %>
.element {
background-color: red;
}
<% end %>
And place that in the head of my page:
# application.html.erb
<head>
<style>
<%= content_for(:style) %>
</style>
</head>
But the element partials are fragment cached, content_for
is ignored in caches.
It's stated in Rails documentation that content_for
will not for work elements that are fragment cached:
WARNING: content_for is ignored in caches. So you shouldn't use it for elements that will be fragment cached.
http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for
Is there currently a way to get content_for
to work with fragment caching on Rails 5.1? No ones seems to have really touched this issue for a while. Does anyone know if there's a reason why?
There are some older mentions here:
Is there a workaround for ignored content_for blocks with caches_action and :layout => false?
content_for works in development but not production?
https://gist.github.com/stackng/891895
https://rails.lighthouseapp.com/projects/8994/tickets/3409-content_for-and-fragment-caching