0

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
  • did you try removing :addresses from the case params and just using addresses_attributes as it is? This post might help http://stackoverflow.com/questions/18540679/rails-4-accepts-nested-attributes-for-and-mass-assignment – mmartinson Apr 12 '15 at 23:03

1 Answers1

0

If an attribute is a hash you need to specify this:

addresses: [ ]

in your params. I think you can use:

params.require(:case).permit(addresses: [:type, :street_adress, :city, :zip, :state])

And then you can remove the nested_attribute from your model as well.

davidwessman
  • 1,099
  • 7
  • 23