Open Quintasan opened 3 years ago
I'm getting same error when I use this:
ApplicationController.renderer.render(
partial: 'shared/item',
locals: { item: item }
)
and my file is /view/shared/_item.json.jb
Only tested this for the simple ApplicationController.render(partial: ...
case on Rails 7.0.4, but in case anyone else finds themselves here this patch seems to get it working:
module Jb
module PartialRenderer
module JSONizer
def render_partial_template(*)
rendered_template = super
rendered_template.instance_variable_set :@body, rendered_template.body.to_json if rendered_template.template.respond_to?(:handler) && (rendered_template.template.handler == Jb::Handler)
rendered_template
end
end
end
end
::ActionView::PartialRenderer.prepend ::Jb::PartialRenderer::JSONizer
The underlying issue is that jb
patches ActionView::TemplateRenderer
, but when you call ApplicationController.render(partial: ...)
it uses ActionView::PartialRenderer
.
Note however that with this patch, calls to render(partial: ...
within another view/partial will now also return JSON, which could mess things up with nested partials. Not sure of a good workaround yet other than using JSON.parse
inside the views.
This action tries to render
app/views/teas/_tea.json.jbuilder
but fails with
unless I append
.to_json
to the partial. Is this working as intended?