rails 多选的一个小tip

<%= f.select :order_status,
        options_for_select(collection, value),
        {}, class: 'form-control', multiple: true %>

这里无论怎么选择,都会提交成这种["", ........] ,烦了我好久。 就困于我,遂google之:stackoverflow,有一条Answers,告诉我需要看:https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select。 终于,在gotcha中找到了答案:

When no selection is made for a collection of checkboxes most web browsers will not send any value. If no category_ids are selected then we can safely assume this field will not be updated. This is possible thanks to a hidden field generated by the helper method for every collection of checkboxes. This hidden field is given the same field name as the checkboxes with a blank value. In the rare case you don’t want this hidden field, you can pass the include_hidden: false option to the helper method.