4

I'm getting a Unpermitted parameters: latitude, longitude, address error in the log when I try to accept nested attributes from a form. The exact params look like:

{  
  "widget"=> {
    "owner"=>"100", 
    "name"=>"Widget Co", 
    "locations_attributes" => {
      "0"=> {
        "latitude"=>"51.4794259", 
        "longitude"=>"-0.1026201", 
        "address"=>"123 Fake Street"
      }
    }
  },
  "commit"=>"Create Supplier", 
  "action"=>"create", 
  "controller"=>"widgets"
}

A widget has_many locations, and a location belongs_to a widget. The params are set in the widgets_controller which I thought would permit everything under "0", but doesn't seem to?

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [{"0" => []}])
end

Is there a working / better way to accept these params?

Thanks

Bebbs
  • 289
  • 2
  • 13

2 Answers2

5

Have a try with this

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [:id, :latitude, :longitude, :address])
end
Pavan
  • 33,316
  • 7
  • 50
  • 76
1

Your widgets_params code should be:

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [:latitude, :longitude, :address])
end
Malik Shahzad
  • 6,703
  • 3
  • 37
  • 49