Collection :as => 'check_boxes' generates extra 'none' hidden field #633

nickurban opened this Issue Jul 25, 2011 · 6 comments

3 participants


When generating

form.input :comments, :as => 'check_boxes', :selected => @comments, :required => false

I get this extra field:

<input id="comments_none" name="search[comments][]" type="hidden" value="">

That's right due the HTML behavior.

When you submit the search comments empty, a blank array will be sent instead of nothing.

@sobrinho sobrinho closed this Jul 25, 2011

Yes but in all other cases I end up with an unexpected empty element in my array. It's annoying, but all multi-select type form elements need to be handled specially. Following the principle of least surprise, would be better to leave it alone?


@nickurban you should use something like array.reject(&:blank?) in your case.

But, activerecord will do it automatically on associations.

I don't remember if it can be disabled on rails helpers or formtastic.

Maybe @justinfrench know :)


@nickurban, @sobrinho This is not an issue with Rails helpers because Rails doesn't have a helper for rendering a group of checkboxes. I don't believe there's an option in Formtastic either.

If I remember correctly...

This feature was added in 7b5ed06, the reason for it (I think) is that if you were to uncheck all checkboxes for the categories association when editing a Book, the browser would not submit any params relating to categories. The result was that the existing category associations would not be removed from the Book when saved.

This is also very "surprising" for developers and users.

The common work-around (at least at the time when this feature was introduced) was to place a hidden input in there first. This patch/feature removed that step for developers.

The key thing for me is that you'll have to do some work, somewhere if you want this stuff to really work. The current behaviour was thought be be the simplest strategy at the time. There may be better strategies, there may be a regression, but it's not as simple as "you have a blank option there, please remove".

Long answer, but I hope it helps :)


I understand why you did it now, and that it's not an error.

It sucks when you get bit by the disappearing box group issue, but that's generally something that comes up the first time you write a web app that uses grouped check boxes (or a multi-select). Once you've used them once, you're aware of the issue. This, in contrast, is surprising to people who are already familiar with multi-select type inputs. Given that, I would probably lean towards surprising-in-the-same-way-as-normal.

Maybe rails should include this kind of functionality and automatically delete the param, but that would probably raise other issues...


Yeah, I'm not 100% sold either way, but I would say that I'm not inclined to change until the next major release at the very earliest, given we're well into RCs for 2.0. Happy for you to re-open and rally some support for reverting the change in 2.1 or 3.0 (not as far away as it sounds).

Thanks for the feedback either way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment