我正在构建一个 Rails 3.2 应用程序,并且正在使用 Rabl 模板。我想添加一个显示记录总数的“根节点”。下面的解决方案将总节点添加到每条记录。我希望它在所有记录之上。
collection @projects
extends "projects/_base"
node(:total_entries) { @projects.total_count }
我希望它是这样的:
- total_entries: 3
-- Entry 1
-- Entry 2
-- Entry 3
我正在构建一个 Rails 3.2 应用程序,并且正在使用 Rabl 模板。我想添加一个显示记录总数的“根节点”。下面的解决方案将总节点添加到每条记录。我希望它在所有记录之上。
collection @projects
extends "projects/_base"
node(:total_entries) { @projects.total_count }
我希望它是这样的:
- total_entries: 3
-- Entry 1
-- Entry 2
-- Entry 3
从READM E 中的一个例子:
object false
node(:total_entries) { @foos.count }
child(@foos, :object_root => false) { attributes :name }
=>
{
"total_entries": 2,
"foos": [
{
"name": "Jane"
},
{
"name": "John"
}
]
}
或者不要将object_rootin 设置为生成:
{
"total_entries": 2,
"foos": [
{
"foo": {
"name": "Jane"
}
},
{
"foo": {
"name": "John"
}
}
]
}