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?
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_hidden option will be added in Rails 4
Thank you. So until Rails 4 comes out, everyone will have his own way of dealing with the problem.
@dalizard I think you can close this issue as it's not a SimpleForm's issue actually :)
Agreed! :)
Could you please explain how I can pass include_hidden option in this example?
f.input :resolutions, :collection => ["SD", "ED", "HD"], :as => :check_boxes
@leoniddinershtein sorry, looks like I forgot to add this option to collection_check_boxes helper 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:
f.input :resolutions, :collection => ["SD", "ED", "HD"], :as => :check_boxes, :include_hidden => false
but right now it works only with Rails's select helper I guess...
@nashby thank you
This question arose from my looking at this issue, but it's a bit more global in scope. I see, @nashby, that your commit was pulled into rails master, as evidenced in the PR you linked to.
However, the behavior's not working in my current rails app which is relatively up-to-date. So my question is this, for anyone more github-savvy than I:
Given that I'd like to have commit 106c988 involved in my own rails, how can I find out which version or release of rails it is included in (or will be)?
So the general question would be something like: given a commit SHA and a particular github repo such as rails/rails, how can I determine which releases of that repo include that commit?
It is very easy. You enter in the commit page like this:
https://github.com/rails/rails/commit/106c988c10c29332343d8de5719a8b045d093753
And look the branch/tag information here:

Wow - that really is easy. For others' future reference, here is what that section of the view looks like when it's in a release:

still I am having the problem even I used include_blank to false , How could fix this problem to not to have hidden inputs for check boxes
= f.input :to, as: :check_boxes, collection: @debt, label: 'To debt', :input_html => { :name => 'to_debt[]' }, :include_hidden => false
The option should be inside the input_html hash and it depends in the
version of Rails that supports this option
On Tue, Dec 16, 2014, 02:07 vkeziah [email protected] wrote:
still I am having the problem even I used include_blank to false , How
could fix this problem to not to have hidden inputs for check boxes= f.input :to, as: :check_boxes, collection: @debt
https://github.com/debt, label: 'To debt', :input_html => { :name =>
'to_debt[]' }, :include_hidden => false—
Reply to this email directly or view it on GitHub
https://github.com/plataformatec/simple_form/issues/603#issuecomment-67109915
.
Thanks for your reply , however its not working I tried to put it inside the input_html still getting the same issue . I am using rails 4.1.6 , can you assist further
= f.input :to, as: :check_boxes, collection: @debt, label: 'To debt', :input_html => { :name => 'to_debt[]' , :include_hidden => false }
@vkeziah it is present only at Rails 4.2.0
If you remove the hidden field via include_hidden: false you will not get anything from the browser when no checkbox is selected. This is a problem when a checkbox had been checked before and you would like to uncheck all boxes now, which will not work. You can try it.
In stead I suggest you clean the array before you save it:
class Movie < ActiveRecord::Base
serialize :resolutions, Array
before_save :clean_resolutions
def clean_resolutions
resolutions.reject { |r| r.blank? }
end
end
@eikes Thank your suggestion, for your code snippet, need to change reject method to reject!. Otherwise it won't save it to db.
class Movie < ActiveRecord::Base
serialize :resolutions, Array
before_save :clean_resolutions
def clean_resolutions
resolutions.reject! { |r| r.blank? }
end
end
I think we can automatically add this option when the input is disabled or we will have all usual options disabled but the hidden won't which means the collection would always be reset upon save.
I use a custom collection_checkboxes input on my project and I have overriden the apply_default_collection_options! method like this
def apply_default_collection_options!(options)
super
options[:include_hidden] = !options[:disabled]
# other options related to my custom checkboxes
end
I believe this issue persists with rails 5.0.0.1. I am adding include_hidden: false but i still get empty values
= f.association :answers, label: false,
collection: @poll.questions[f.options[:child_index].to_i].answers,
as: :check_boxes, include_hidden: false
Still have the same issue with a select. include_hidden: false has no effect.
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 %>
Most helpful comment
I believe this issue persists with rails 5.0.0.1. I am adding include_hidden: false but i still get empty values