Skip to content

Commit

Permalink
Check boxes now return an array, but it still generate hidden fields.…
Browse files Browse the repository at this point in the history
… The hidden fields are required to use Rails internal logic to mark the checkbox as checked or not which is better then reproducing the logic on our own. By default hidden fields return blank, which is automatically ignored by association_ids.
  • Loading branch information
josevalim committed May 12, 2009
1 parent 004e1c0 commit 3f00af0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions lib/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -796,17 +796,16 @@ def check_boxes_input(method, options)
collection = find_collection_for_column(method, options)
html_options = options.delete(:input_html) || {}

input_name = generate_association_input_name(method)

input_name = generate_association_input_name(method)
value_as_class = options.delete(:value_as_class)
unchecked_value = options.delete(:unchecked_value) || ''

html_options = { :name => "#{@object_name}[#{input_name}][]" }.merge(html_options)

list_item_content = collection.map do |c|
label = c.is_a?(Array) ? c.first : c
value = c.is_a?(Array) ? c.last : c

html_options.merge!(:name => "#{@object_name}[#{input_name}][#{value.to_s.downcase}]",
:id => generate_html_id(input_name, value.to_s.downcase))
html_options.merge!(:id => generate_html_id(input_name, value.to_s.downcase))

li_content = template.content_tag(:label,
"#{self.check_box(input_name, html_options, value, unchecked_value)} #{label}",
Expand Down
4 changes: 2 additions & 2 deletions spec/formtastic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,7 @@ def custom(arg1, arg2, options = {})
it 'should have a checkbox input for each post' do
Post.find(:all).each do |post|
output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][#{post.id}]']", :count => 2)
output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
end
end

Expand Down Expand Up @@ -1595,7 +1595,7 @@ def custom(arg1, arg2, options = {})
output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][#{author.id}]']")
output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
end
end
end
Expand Down

0 comments on commit 3f00af0

Please sign in to comment.