Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 Multiple Nested Forms and Strong Parameters

I am using rails 4 and nested forms and strong parameters.

I need to update multiple models from one form.

This is how I am declaring my strong parameters. From the Parent controller. The associations are has_many and belongs_to going in this order Rundatum->Material->ParticleSize

def rundatum_params
    params.require(:rundatum).permit( :material, :company_id, :material_density, :feed_moisture, :date, :building, :machine, :material_weight, :time_mins, :rate_lb_hr, :mill_amps, :class_amps, :mill_liner, :beater_plate_size, :mill_rpm, :class_rpm, :feeder_type, :feeder_setting, :feeder_aug_diameter, :tlgs_set, :air_pressure, :temp_mill_out, :temp_prod_out, :temp_ambient, 

    materials_attributes: [:id, :name, :density, :msds_url, :moisture, :notes, :_destroy], 

    particle_sizes_attributes: [:id, :screen, :percent_through, :percent_retained, :_destroy])
end

The output from the rails server is:

Rundatum Load (0.6ms)  SELECT  "rundata".* FROM "rundata"  WHERE "rundata"."id" = $1 LIMIT 1  [["id", 7]]

Unpermitted parameters: particle_sizes_attributes

What is the way to declare strong parameters when updating multiple models from one form?

Thanks

like image 708
JerryA Avatar asked Nov 03 '14 19:11

JerryA


1 Answers

If you have ParticleSize nested within Material, then you should nest your parameters as well.

def rundatum_params
  params.require(:rundatum).permit( :material, :company_id, :material_density, :feed_moisture, :date, :building, :machine, :material_weight, :time_mins, :rate_lb_hr, :mill_amps, :class_amps, :mill_liner, :beater_plate_size, :mill_rpm, :class_rpm, :feeder_type, :feeder_setting, :feeder_aug_diameter, :tlgs_set, :air_pressure, :temp_mill_out, :temp_prod_out, :temp_ambient, 

  { materials_attributes: [:id, :name, :density, :msds_url, :moisture, :notes, :_destroy, 

  { particle_sizes_attributes: [:id, :screen, :percent_through, :percent_retained, :_destroy] }] })
end
like image 56
ptd Avatar answered Oct 20 '22 19:10

ptd