Rails 5 nested form won't create child records -
i've been stuck on hours. don't understand how nested forms , using 'accepts_nested_attributes_for' supposed work.
i have 'form_for' new list, , 'fields_for' it's list_items. here fields_for list_items.
fields_for :list_items
now, 'list_item' :through part of list's has_many_through associations user's 'user_items'. nested form supposed submit information 'user_item' needed build 'list_item' belong new list. seems somewhere along way parameters child items not being delivered...
started post "/list" 127.0.0.1 @ 2017-08-17 18:18:04 -0400 processing listscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"203d1bpqzzku9j0gyupovqnfbkxmreydlmb4idaradnjvx5fqcydg5due/d2ngwocu6afkc+hwl5qcpfu9glqw==", "list"=>{"name"=>"", "list_items_attributes"=>{"0"=>{"user_item_id"=>"1", "quantity"=>"1"}, "1"=>{"user_item_id"=>"2", "quantity"=>"1"}, "2"=>{"quantity"=>"1"}, "3"=>{"quantity"=>"1"}, "4"=>{"quantity"=>"1"}, "5"=>{"quantity"=>"1"}, "6"=>{"quantity"=>"1"}, "7"=>{"quantity"=>"1"}, "8"=>{"quantity"=>"1"}}}, "commit"=>"submit"}
and instead @list fails save , 2 flash error messages pop up:
["list items list must exist", "list items user item must exist"]
this tells me somehow list_items aren't being build correctly, or fields isn't right...or something. stuck. anyway, here's lists controller:
and list model:
and again fields_for :list_items
i hope makes sense. thanks
update
something noticed in parameters being sent form nested params hash appears part array...?
"list_items_attributes"=>{"0"=>{"user_item_id"=>"1", "quantity"=>"1"}, "1"=>{"user_item_id"=>"2", "quantity"=>"1"}
this not reflected in list_params , may issue...still not sure fix though:
def list_params params.require(:list).permit(:name, list_items_attributes: {:user_item_id, :quantity}) end
Comments
Post a Comment