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: [])
Now, the problem is that if I send an empty array of foo_ids
, the parameter is ignored. Instead of clearing all foos
as an empty array should do, the association is left alone, because foo_ids
isn't permitted.
This may be because an empty array is converted to nil in rails, and that nil value is ignored as strong parameters is looking for an array of scalar values, not a single scalar value.
Can anyone suggest a good way to solve this? Thanks!
Additional info
In an update controller action, I need to be able to handle two cases. I need to be able to set foo_ids
to an empty array. I also need to be able to ignore foo_ids
if I merely want to update another field. Setting foo_ids
to an empty array if nil does not work for this second case.
question from:
https://stackoverflow.com/questions/20164354/rails-strong-parameters-with-empty-arrays 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…