Render json with layout
I try to do this:
# layout.json.jbuilder
json.merge! JSON.parse(yield)
json.status do
json.code response.code
json.message response.message
end
json.errors @errors || []
# in my controller
render json: {}, layout: true
This renders {} when I would want {status: {...}, errors: []}
Is it possible?
Well, when you call render json: something, you're effectively telling your controller to completely bypass view. Try removing explicit render call or replace it with just render layout: true.
Fine, but it doesn't solve my problem. render layout: true will fail because of the yield in the layout. It will complain there is nothing to render.
Why could I do that with text render text: "bla", template: true and not with json?
Actually, I can even do render text: '{"a": "a"}', layout: true and it will work but that's really hacky!
Did anyone ever figure out an answer to this?
render json: {...}, layout: true is still not rendering the jbuilder layout file.