2017-06-14 92 views
0

我正在玩Rails API服务器,我试图将一个JSON对象传递给数据库,但它保持导致一个空值。试图通过json散列postgres 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 

这是我在邮差

{ 
    "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"它会写没有问题。我只是在邮差中传递错误的数据还是其他的东西?

回答

1

您的强大参数必须调整以期望并传递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 
+0

完美的作品,非常感谢你! – oneWorkingHeadphone