n_x_l
n_x_l

Reputation: 1610

Rails Strong Parameters - POST a json array + one extra root field

I am not sure what I am doing wrong here. I need to post an json object containing an array of objects + one extra root field. So for this code:

{
    "root_field": "somedata",
    "myobjects": [
        {
            "attr1": "1",
            "attr2": 2",
            "attr3": "3"
        },
        {
            "attr1": "1",
            "attr2": "2",
            "attr3": "3"
        }
    ]
}

I have this as code for strong parameters, which allows all myobjects in but fails to pass on the root_field, which is important in my application.

def my_params
  params.require(:root_field)
  params.require(:myobjects).map do |e|
    ActionController::Parameters.new(e.to_hash).permit(:attr1,:attr2,:attr3)
  end
end

Any thoughts?

Upvotes: 2

Views: 1178

Answers (1)

Baldrick
Baldrick

Reputation: 24340

If you want to while list root_field as a scalar value, it should be

params.permit(:root_field)
params.permit(:myobjects => [:attr1,:attr2,:attr3])

Upvotes: 2

Related Questions