gpt4 book ai didi

ruby-on-rails - 尝试将 json 哈希传递给 postgres json 列导致空值

转载 作者:行者123 更新时间:2023-11-29 12:55:25 25 4
gpt4 key购买 nike

我正在使用 Rails API 服务器,我试图将 JSON 对象传递给数据库,但结果始终为空值。

架构很简单:

create_table "notes", force: :cascade do |t|
t.string "title"
t.string "created_by"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.json "clients"
end

这是我在 Postman 中发布的内容

{
"created_by": "1",
"title": "I'm another note",
"clients": {
"client1": {
"name": "name 1",
"role": "role 1"
},
"client 2": {
"name": "name 2",
"role": "role 2"
}
}
}

这会返回

{
"id": 14,
"title": "I'm another note",
"created_by": "1",
"created_at": "2017-06-14T10:52:00.226Z",
"updated_at": "2017-06-14T10:52:00.226Z",
"clients": null
}

如果我将客户端键/值对作为 "clients": "some client" 发送,它将不会出现问题。我只是在 Postman 中传递了错误的数据还是其他原因?

最佳答案

必须调整您的强参数以期望并传递 json。

要允许接受带有任何键的 json,您可以将 note_params 设置为

def note_params
params.require(:mote).permit(:title, :created_by).tap do |white_list|
white_list[:clients] = params[:note][:clients].permit! if params[:note][:clients]
end
end

关于ruby-on-rails - 尝试将 json 哈希传递给 postgres json 列导致空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44542786/

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