在我的rails项目中,我使用RABL gem来进行json响应。 我目前的回应如下:
[
{
"id": 25,
"name": "Follow ups",
"steps": [
{
"id": 67,
"Step 1 Subject: ": "Email Test"
},
{
"id": 65,
"Step 1 Subject: ": "Email Test"
},
{
"id": 66,
"Step 1 Subject: ": "Email Test"
}
]
}
我想在步骤名称键中使用计数器,如下所示:
[
{
"id": 25,
"name": "Follow ups",
"steps": [
{
"id": 67,
"Step 1 Subject: ": "Email Test"
},
{
"id": 65,
"Step 2 Subject: ": "Email Test"
},
{
"id": 66,
"Step 3 Subject: ": "Email Test"
}
]
}
如何在此档案中运行计数器?
答案 0 :(得分:0)
为您的Step类添加索引属性(这不应该是db列)。然后,您可以在控制器方法中执行此类预处理。使用rabl文件中的index属性。
def index
@items = get_items
@items.each do |item|
item.steps.each_with_index do |step, index|
step.index = index
end
end
end