Tutorial :How do I allow a user to select zero options from a multiple selection box in rails?


I have a settings model with a column options, and set it to serialize with serialize :options. In my view, I have a multiple selection box, using select("settings", "options", ['option1','option2','option3'], {}, :multiple => true) which works fine so long as the user selects at least one option. However, if they don't select any options, no options are submitted and so the options aren't updated.

How do I allow the user to select zero options from a multiple selection box in rails?


I do not like assuming a value is empty if the attribute is not posted. It breaks the way Rails expects to update attributes, and it can present problems if you are using your controller actions also for APIs as well as HTML. My preferred way of handling this is by adding a hidden input field before multiselects.

<input type="hidden" value="" name="parent_model[my_attribute_ids][]">  

If you use JQuery you can automate the addition of these hidden input fields:

$('select[multiple="multiple"]').each(function(i){      $(this).before('<input type="hidden" name="'+this.name+'" value="" />')  });  

I realize this answer is not very timely, but I hope this will help someone with a similar question.


That has nothing to do with rails: html form won't send such parameter to server if nothing is chosen in 'select' element. But you should be able to fix it in controller. Something like this

if params[:settings] == nil    params[:settings] = [];  end  

Not sure if there's more elegant solution.


Add a hidden field after the select box, that posts an empty value to "settings[options]"

It's same trick that rails uses to make sure unchecked checkboxes get posted as false.


You could provide a "None" option.

Example from: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, {:multiple => true})  

Would become

<select name="post[person_id]" multiple="multiple">    <option value="">None</option>    <option value="1">David</option>    <option value="2" selected="selected">Sam</option>    <option value="3">Tobias</option>  </select>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »