在rabl中渲染没有节点标签的子节点

时间:2013-04-12 16:53:37

标签: ruby-on-rails ruby sinatra rabl

我正在使用rabl在Sinatra中构建和API。 所需的JSON des不接受某些节点中的标签 使用这个Rabl模板:

object @user
  attributes :profile_photo => :profile_photo, :name => :first_name, :last_name =>       :last_name
  child :addresses do
  attributes :id 
    child :country do
      attributes :name
    end
  end

我得到以下JSON:

{
user: {
profile_photo: null,
first_name: "John",
last_name: "Doe",
addresses: [
{
address: {
id: 43,
country: {
name: "Iceland"
}
}
},
{
address: {
id: 44,
country: {
name: "Cambodia"
}
}
},
{
address: {
id: 45,
country: {
name: "North Korea"
}
}
}
]
}
}

我希望得到的是:

{
user: {
profile_photo: null,
first_name: "John",
last_name: "Doe",
addresses: [
{
{
id: 43,
country: {
name: "Iceland"
}
}
},
{
{
id: 44,
country: {
name: "Cambodia"
}
}
},
{
{
id: 45,
country: {
name: "North Korea"
}
}
}
]
}
}

在Rabl有办法吗?

1 个答案:

答案 0 :(得分:0)

我必须将Rabl配置设置为:

Rabl.configure do |config|
  config.include_json_root = false
  config.include_child_root = false
end

我得到了另一个Stack溢出线程的答案:

Removing child root nodes in RABL