Rails - Strong parameters with empty arrays

后端 未结 5 1727
星月不相逢
星月不相逢 2021-02-01 13:37

I\'m sending an array of association ids, say foo_ids to my controller. To permit an array of values, I use:

params.permit(foo_ids: [])
5条回答
  •  鱼传尺愫
    2021-02-01 14:38

    I ran into the same issue and found a similar solution as Donato, albeit while constructing a multipart FormData in JS. The trick was to put an empty string in the array.

    const formData = new FormData()
    formData.append('dish[tag_ids][]', '')
    

    On the controller side, params arrives with "dish"=>{"tag_ids"=>[""]}, which dish.update interprets as "remove all tags".

提交回复
热议问题