simple_form: Check boxes and hidden input problem
Hi guys!
I am using simple_form 2.0.2 with rails 3.2.3. I have a simple model called Movie with a serialized field ‘resolutions’:
class Movie < ActiveRecord::Base
serialize :resolutions, Array
end
The simple_form looks like this:
<%= simple_form_for @movie do |f| %>
<%= f.input :resolutions, :collection => ["SD", "ED", "HD"], :as => :check_boxes %>
<% end %>
The problem is, that simple_form adds the following input tag (with no value) after the 3 input tags for the collection:
<input name="movie[resolution][]" type="hidden" value>
When the form gets submitted with only the “SD” checkbox selected, the params looks like this:
"resolutions"=>["SD", ""]
and the Movie instance gets updated accordingly:
irb(main):022:0> m.resolutions
=> ["SD", ""]
The problem is that, I don’t want the empty string present in the resolutions field. And in case all checkboxes are uncheck, the params contains “resolutions”=>[“”] instead of “resolutions”=>[]. Any suggestions?
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Reactions: 5
- Comments: 21
I believe this issue persists with rails 5.0.0.1. I am adding include_hidden: false but i still get empty values
Still have the same issue with a select.
include_hidden: falsehas no effect.It’s not a SimpleForm’s thing but Rails’s. You can read a bit about this here https://github.com/rails/rails/issues/5402 Note,
:include_hiddenoption will be added in Rails 4@leoniddinershtein sorry, looks like I forgot to add this option to
collection_check_boxeshelper in Rails (or it didn’t exist at that time). Anyway, I made a PR to Rails https://github.com/rails/rails/pull/12662. So after it releases you’ll be able to use it like this:but right now it works only with Rails’s
selecthelper I guess…Using latest version of rails this worked it for me ( using simple_form ) as it removed the leading “” in the hash.
<%= f.collection_check_boxes :tags, Tag.all, :tag, :tag, as: :boolean, include_hidden: false %>
@eikes Thank your suggestion, for your code snippet, need to change
rejectmethod toreject!. Otherwise it won’t save it to db.