Search code examples
ruby-on-railsrubyjsonjbuilder

Jbuilder Partials Merge Instead of Nest


I'm attempting to have a json response that looks like the below:

{
    id: 3,
    title: "Magic",
    desc: "A bag of coolness!"
    type: {
        id: 14,
        title: "Dust"
    }
}

What I get is:

{
    id:14,
    title:"Dust",
    desc:"A bag of coolness!"
    type: null
}

The three jbuilder files being used are below:

_item.json.jbuilder

json.(item, :id, :title, :desc)
json.type json.partial! item.type

show.json.jbuilder

json.partial! @item

_type.json.jbuilder

json.(type, :id, :title)

Why is jbuilder merging type and item instead of nesting type? How do I prevent this?


Solution

  • To nest a partial, the below code will work:

    json.type do
        json.partial! item.type
    end