【问题标题】:Jbuilder Partials Merge Instead of NestJbuilder Partials 合并而不是 Nest
【发布时间】:2015-06-07 16:42:36
【问题描述】:

我正在尝试获得如下所示的 json 响应:

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

我得到的是:

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

使用的三个jbuilder文件如下:

_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)

为什么 jbuilder 合并类型和项而不是嵌套类型?如何防止这种情况发生?

【问题讨论】:

    标签: ruby-on-rails ruby json jbuilder


    【解决方案1】:

    要嵌套一个部分,下面的代码将起作用:

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

    【讨论】:

      猜你喜欢
      • 2016-02-15
      • 2010-09-28
      • 1970-01-01
      • 2018-11-14
      • 2022-12-15
      • 2015-03-12
      • 2014-08-29
      相关资源
      最近更新 更多