I'm using Rails 3.2.0.rc2. I've got a Model
, in which I have a static Array
which I'm offering up through a form such that users may select a subset of Array
and save their selection to the database, stored in a single column in Model
. I've used serialize on the database column which stores the Array
and Rails is correctly converting the users' selections into Yaml (and back to an array when reading that column). I'm using a multi-select form input to make selections.
My problem is that, the way I currently have it, everything works as I would expect except that the user's subset array always has a blank first element when it's sent to the server.
This isn't a big deal, and I could write code to cut that out after the fact, but I feel like I'm just making some kind of syntactical error as it doesn't seem to me that the default Rails behaviour would intentionally add this blank element without some reason. I must have missed something or forgot to disable some kind of setting. Please help me understand what I'm missing (or point me in to some good documentation that describes this with more depth than what I've been able to find on the intertubes).
subset_array
which is a TEXT fieldserialize :subset_array
ALL_POSSIBLE_VALUES = [value1, value2, value3, ...]
f.select :subset_array, Model::ALL_POSSIBLE_VALUES, {}, :multiple => true, :selected => @model.subset_array
"model" => { "subset_array" => ["", value1, value3] }
UPDATE 'models' SET 'subset_array' = '--- \n- \"\"\n- value1\n- value3\n'
As you can see, there's this extra, blank, element in the array being sent and set in the database. How do I get rid of that? Is there a parameter I'm missing from my f.select
call?
Much thanks appreciated :)
EDIT: This is the generated HTML code from the f.select
statement. It looks as though there is a hidden input being generated which may be the cause of my issue? Why is that there?
<input name="model[subset_array][]" type="hidden" value> <select id="model_subset_array" multiple="multiple" name="model[subset_array][]" selected="selected"> <option value="value1" selected="selected">Value1</option> <option value="value2">Value2</option> <option value="value3" selected="selected">Value3</option> <option...>...</option> </select>
In Rails 4:
You will be able to pass :include_hidden
option. https://github.com/rails/rails/pull/5414/files
As a quick fix for now: you can use right now in your model:
before_validation do |model| model.subset_array.reject!(&:blank?) if model.subset_array end
This will just delete all blank values at model level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With