0

我的 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可用。

我怎样才能在上面的代码中获得父母属性?显然,只要执行以下操作就会返回 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"
}
},
4

1 回答 1

0

您可以添加一个添加用户数据的自定义节点:

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
于 2013-06-26T18:45:40.370 回答