gpt4 book ai didi

ruby-on-rails - 渲染 :json with extra data

转载 作者:行者123 更新时间:2023-12-04 01:23:27 25 4
gpt4 key购买 nike

我有一个名为 Todo 的模型,我渲染了这个:

format.json { render :json => @todo }

每个 Todo 都属于一个列表。我想添加 @todo.list.completion_percentage 的值到 JSON,因为我需要它来更新 UI(AJAX 请求),所以 JSON 看起来像这样:
{
"todo": {
"created_at": "2011-02-26T19:39:43Z",
"updated_at": "2011-02-26T19:53:13Z",
"done": true,
"text": "Apples",
"id": 10,
"list_id": 2,
"user_id": 1,
"due_date": null



// BELOW THIS LINE SHOULD BE IMPLEMENTED
"list": {
"completion_percentage": 63
}
}
}

我尝试了各种事情,但没有奏效。谁能帮我?

最佳答案

您可能希望捕获 JSON 数据的值,然后对其进行修改。

format.json { render :json => JSON::parse(@todo.to_json).merge("list" => { "completion_percentage" => 63 }).to_json }

关于ruby-on-rails - 渲染 :json with extra data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5129356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com