How to enable strong parameters for nested attributes?

I get an error Unpermitted parameters: latitude, longitude, addressin the log when I try to accept nested attributes from a form. The exact parameters look like this:

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

has_manyLocation widget and widget location belongs_to. The parameters are set in widgets_controller, which, as I thought, will allow everything under "0", but it doesn't seem to?

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

Is there a working / best way to accept these options?

thank

+3
source share
2 answers

Try with this

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [:id, :latitude, :longitude, :address])
end
+4
source

Your widgets_params code should be:

def widget_params
  params.require(:widget).permit(:owner, :name, locations_attributes: [:latitude, :longitude, :address])
end
+1
source

Source: https://habr.com/ru/post/1687914/


All Articles