2017-06-14 90 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