1

I'm playing with the Rails API server and I'm trying to pass a JSON object to the database, but it keeps resulting in a null value.

The schema is simple:

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

Here's what I'm posting in 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"
        }
    }
}

and this returns

{
  "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
}

If I send the client key/value pairs as "clients": "some client" it will write no problem. Am I just passing the wrong data in Postman or is it something else?

1 Answer 1

2

Your strong parameters have to be adjusted to expect and pass a json.

To allow a json with any keys to be accepted, you would set note_params as

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.