I am setting up a formtastic form where a number of fields need to have the option to “select all that apply”. (FYI I use slim but i can convert this back to normal rails if necessary).
For example. I define this input:
= f.input :categories_list, :label => _("Commonly Found In"), :as => :check_boxes, :multiple => true, :collection => Category.all
This creates a bunch of checkboxes, and only ones that exist appear. BUT when I select one (or two or three) it breaks the creation of the model that I am creating, and I noticed that the categories_list field is submitted as:
"categories_list"=>["", "1", "2"],
Instead of as:
"categories_list"=>["1", "2"],
Does anyone know why this is? Is this a bug with formtastic? Please let me know if I should post any more relevant code. I would really like to know how to make this work.
This is to allow clearing of all selections. Without the blank/empty value at the start of the array, the browser would not set the
categories_listparam at all, and the existing checkboxes would remain checked.With
categories_listnot set, you’d have no idea what the user intended.You simply need to filter this blank option out of the params either in the controller or your model if it’s not working for you (but I’d recommend you try to replicate the issue on editing before dismissing it).