jbuilder icon indicating copy to clipboard operation
jbuilder copied to clipboard

Render json with layout

Open karellm opened this issue 11 years ago • 3 comments

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?

karellm avatar Dec 27 '14 19:12 karellm

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.

rwz avatar Dec 29 '14 18:12 rwz

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!

karellm avatar Dec 29 '14 23:12 karellm

Did anyone ever figure out an answer to this?

render json: {...}, layout: true is still not rendering the jbuilder layout file.

TWGerard avatar Jun 14 '19 17:06 TWGerard