Gon works nicely with Jbuilder. In particular, the documentation tells to use it like this
gon.jbuilder template: 'path/to/template.json.jbuilder'
This works nicely, but I would like to cache the template result so there is no need to re-render the template. Hence, I used render_to_string, like this:
gon.entities = Rails.cache.fetch('entities_json') do
JSON.parse render_to_string(template: 'path/to/template.json.jbuilder')
end
The method returns the appropriate string, I have to pass it through JSON.parse, otherwise the gon variable stores a double-encoded JSON string. This is annoying, but I don't know how else to solve it.
Unfortunately, this invocation to render_to_string results in the whole HTML page being rendered as a string. The HTML is complete, the gon variables have the expected values, but out of a sudden, the page is not displayed as a HTML anymore in Chrome.
Any idea how to solve this?