[Feature request] Ability to use content_for within a fragment cache block

I’ve just been bitten by this documented but surprising and very easy-to-forget behavior (can’t use content_for within block that will be cached) as many others have before, and I’ve been trying to figure out the current status of fixing it…

From what I can tell, this is still a problem in current version Rails but it looks like there has not been any progress or merge request opened since this one from 2009: #3409 [PATCH]content_for and fragment caching - Ruby on Rails - rails

There are various gists such as make action and fragment caching of rails3 compatible with content_for · GitHub but all the ones I could find are outdated.

Does anyone have a patch for this that is known to work with Rails 5.2+?

It sounds like the core team would be willing to consider a MR that adds this support, but just nobody has submitted one yet … which is surprising.

@matthewd wrote on ruby on rails - Workaround for content_for use in conjunction with fragment caching - Stack Overflow :

It looks like the issue just fell off the radar many years ago: I see no relevant PR in GitHub at all. (I actually have a note about this in my personal todo list, but had no idea it had previously been worked on.)

No , there is no reason why this functionality hasn’t been implemented (if there was a reason 8 years ago, it’s been lost to the sands of time – and may no longer apply anyway). If someone’s interested in pursuing a PR: please do.

At the very least, it should raise an error if you try to do this (proposed here), but really, it would be much better to just add support for it, because it is a reasonable thing to expect, and there is no good workaround.

1 Like

Merge request submitted here: Allow content_for to be used within a fragment cache block by TylerRick · Pull Request #39600 · rails/rails · GitHub