New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixing select[multiple] html specification problem. #1552
Conversation
Generating hidden input with same name before each multiple select
Fixing select[multiple] html specification problem.
[rails#1552 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
def test_select_with_multiple_to_add_hidden_input | ||
output_buffer = select(:post, :category, "", {}, :multiple => true) | ||
assert_dom_equal( | ||
"<input type=\"hidden\" name=\"post[category][]\" value=\"\"/><select multiple=\"multiple\" id=\"post_category\" name=\"post[category][]\"></select>", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bogdan it looks like the value from the hidden field will not be overwritten since the field name contains []
in the end. So for the case when nothing selected it will pass:
{post: {category: [""]}}
and for the case when something was selected:
{post: {category: ["", "Category 1", "Category 2"]}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it works as you said for nothing. As this patch got accepted: this behavior is expected, but it is not perfect as we expect to pass {post: {category: []}}
, but I don't know how to do that.
The case when something is selected: Are you sure?
I would recommend the following workaround anyway: http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde/8939205#8939205
I am sure, I am also seeing this, and its actually kind of annoying. I'd rather not have to clean up params in my controller because of Rails magic silently adding things. I am not even using this for updating a model, but I do use it when trying to query against a list of users belonging to different organizations. This is what comes over when you have one or more selected:
While I would prefer not to have the hidden field generated at all, is there anyway we could throw an option to not have the hidden field generate? |
same problem here |
There, same problem too |
[Jonas Baumann & Yves Senn] The submitted params from a select with `multiple: true` look as follows: ``` {post: {category: [""]}} {post: {category: ["", "Category 1", "Category 2"]}} ``` This is a follow up to #1552.
[Jonas Baumann & Yves Senn] The submitted params from a select with `multiple: true` look as follows: ``` {post: {category: [""]}} {post: {category: ["", "Category 1", "Category 2"]}} ``` This is a follow up to #1552.
There is very good work around for deselected check box in FormHelper#check_box.
I think it's good idea to use same work around for
select[multiple]
that have the same problem:Server do not receive any value when all options got deselected.
So we will generate hidden input with the same name and blank value before each multiple select.