Rails JSON使用RABL,显示父属性?
我的Rails 3.2应用程序中有以下RABL文件:Rails JSON使用RABL,显示父属性?,json,ruby-on-rails-3,gem,rabl,Json,Ruby On Rails 3,Gem,Rabl,我的Rails 3.2应用程序中有以下RABL文件: collection @results.limit(5) attributes :date, :client_id child :client do attributes :Surname end child :animal do attributes :AnimalName end 我想添加结果所属的用户的名称。我在RABL文档中读到的所有内容似乎都表明只有child和node可用 如何在上述代码中获得parents属性?显
collection @results.limit(5)
attributes :date, :client_id
child :client do
attributes :Surname
end
child :animal do
attributes :AnimalName
end
我想添加结果所属的用户的名称。我在RABL文档中读到的所有内容似乎都表明只有child
和node
可用
如何在上述代码中获得parents属性?显然,只要执行以下操作,就会返回NULL
child :animal do
attributes :AnimalName
end
这可能吗
当前JSON输出:
{
date: "2013-06-25T19:36:11+01:00",
client_id: 88172,
client: {
Surname: "Strange"
},
animal: {
AnimalName: "Ria"
}
},
期望输出:
{
date: "2013-06-25T19:36:11+01:00",
client_id: 882372,
client: {
Surname: "Summer"
},
animal: {
AnimalName: "Ria"
},
user: {
UserName: "Danny"
}
},
您可以添加一个自定义节点,用于添加用户数据:
collection @results.limit(5)
attributes :date, :client_id
node :user do |report|
partial('users/show', :object => report.user)
end
这是假设您需要整个用户/显示模板
如果只需要名称键/值:
node :user do |report|
{ :name => report.user.name }
end
感谢您的建议,我已经在我的问题中添加了预期的JSON。我正在尝试
adduseras child
方法,我已经在users中添加了show.JSON.rabl文件,并添加了您的示例代码。它只返回null
。我搞不懂!看起来extends
选项仅用于属性集的模板,但不包含任何数据。results.json只返回user:null
。是否为users/show.rabl设置了rabl模板?如果没有,您可以创建一个属性,或者直接在此模板中为用户输入属性。extensed yadda
就像在普通rails视图中渲染部分一样,但是您需要另一个可用的模板。为了澄清这一点,您的结果和用户模型之间是否存在ActiveRecord关系<代码>属于:用户