This incorporates the code from @fbuenemann; I simply wrote a spec to check for the new hidden input.
My 'hidden_input' branch is based off of 1.2-stable and I rebased @fbuenemann's branch on top of mine.
spec for hidden input when adding a multiple select
Insert hidden field for multiple selects to allow clearing.
This approach is similar to what's needed to clear multiple
Shouldn't the hidden tag come before the select?
create_hidden_field_for_multiple_select(input_name) << select_html
@justinfrench given that the code worked fine for me in production, I don't think that it matters.
I too, thought that the hidden tag should appear before the select, but I just tried it and it worked for me as well.
The approach we've taken elsewhere in the code is to put them first, so I'd like to see this switched around and tested pretty please :)
@kchien you can merge/cherry-pick a096d4b from my branch. This puts the input after the label, but before the select.
Prepend hidden field instead of appending
make the spec for hidden input more specific by checking name attr.
@fbuenemann - Thanks; I cherry-picked that commit , and edited my spec by making the check more specific.
@justinfrench - Please take a look at the modifications and let us know what you think. Thank you!
merged in, thanks!
I can't figure out why do we need hidden fields before each multi-selects? In my application, i systematically get 2 hidden fields before the select, which gives something like that in my controller foo: ["", "", "bar"].
@sailor can you please open a new issue with example code to repeat the issue?
I have the same problem as @sailor have. And I also don't understand why do we need hidden fields.
@dmitry we need hidden inputs because browsers do not post the attribute if none of the checkboxes or multi-select options were chosen. Mass assignment and ActiveRecord's update_attributes no longer knows if you wanted to unselect all options, or to not update that attribute at all. This is the same problem we have with unchecked checkboxes, and Rails "solved" this with a hidden field. We do the same here. If the choices we've made don't make sense to you, you can quickly change this with a custom input.
The issue of having two hidden fields instead of one is definitely a bug (please, someone, give me a test case!), but I don't want to continually debate the idea that we should have none :)
@justinfrench Thank you for the explanation. I though about that case, actually.
FYI, I just want to describe case, when there are no need for the hidden input. An example is, when using meta_search with scopes. As scopes invoked with the params from the search, and params have empty strings (because of multiselect). To fix that, you should check params for presence in every scope that uses multiselect.
@dmitry would love to see a pull request if you've got ideas, thanks!
@justinfrench Investigated that problem a bit.
Hidden input addition was introduced in nashby/rails@9eb6cd6
Then include_hidden was introduced in new rails 4.0: nashby/rails@54a75e1
So hopefully, hidden logic can be removed in the next minor version: 1.3
And after that in new rails it will be possible to use multiple select without hidden field, just including an option: include_hidden: false
What do you think @justinfrench? When the new minor version of formtastic is planned?