I have a RESTful create action in my Rails API for my model Case
. It's very simple:
@case = Case.new(case_params)
if @case.save
render json: @case, status: :created, location: @case
else
render json: @case.errors, status: :unprocessable_entity
end
I am POSTing data to the endpoint (JSON) with the fields for the model as well as associations. When the form is filled out on the frontend, addresses are included. The Case
model has many Address
models.
So I include the addresses in the JSON as an array of objects, ex:
{
"field_on_case": "value",
"addresses": [{
"street_address": "1234 wonderland"
}, {
"street_address": "4321 wonderland"
}]
}
When doing this, and POSTing to the API, in the web server I see: Unpermitted parameters: addresses
I have this snippet in my Case
controller in the case_params
method:
params.require(:case).permit(:addresses, addresses_attributes: [:id, :type, :street_address, :city, :zip, :state])
I also have this line in my Case
model:
accepts_nested_attributes_for :addresses