mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #8108 from Casecommons/fix-multiple-and-index-in-instance-tag
Support :multiple option on input tags that also have :index
This commit is contained in:
commit
c449462f45
3 changed files with 17 additions and 6 deletions
|
@ -80,9 +80,11 @@ module ActionView
|
|||
options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index) }
|
||||
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
|
||||
else
|
||||
options["name"] ||= options.fetch("name"){ options['multiple'] ? tag_name_multiple : tag_name }
|
||||
options["name"] ||= options.fetch("name"){ tag_name }
|
||||
options["id"] = options.fetch("id"){ tag_id }
|
||||
end
|
||||
|
||||
options["name"] += "[]" if options["multiple"]
|
||||
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
|
||||
end
|
||||
|
||||
|
@ -90,10 +92,6 @@ module ActionView
|
|||
"#{@object_name}[#{sanitized_method_name}]"
|
||||
end
|
||||
|
||||
def tag_name_multiple
|
||||
"#{tag_name}[]"
|
||||
end
|
||||
|
||||
def tag_name_with_index(index)
|
||||
"#{@object_name}[#{index}][#{sanitized_method_name}]"
|
||||
end
|
||||
|
|
|
@ -27,7 +27,7 @@ module ActionView
|
|||
|
||||
# Append a hidden field to make sure something will be sent back to the
|
||||
# server if all check boxes are unchecked.
|
||||
hidden = @template_object.hidden_field_tag(tag_name_multiple, "", :id => nil)
|
||||
hidden = @template_object.hidden_field_tag("#{tag_name}[]", "", :id => nil)
|
||||
|
||||
rendered_collection + hidden
|
||||
end
|
||||
|
|
|
@ -525,6 +525,19 @@ class FormHelperTest < ActionView::TestCase
|
|||
)
|
||||
end
|
||||
|
||||
def test_check_box_with_multiple_behavior_and_index
|
||||
@post.comment_ids = [2,3]
|
||||
assert_dom_equal(
|
||||
'<input name="post[foo][comment_ids][]" type="hidden" value="0" /><input id="post_foo_comment_ids_1" name="post[foo][comment_ids][]" type="checkbox" value="1" />',
|
||||
check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1)
|
||||
)
|
||||
assert_dom_equal(
|
||||
'<input name="post[bar][comment_ids][]" type="hidden" value="0" /><input checked="checked" id="post_bar_comment_ids_3" name="post[bar][comment_ids][]" type="checkbox" value="3" />',
|
||||
check_box("post", "comment_ids", { :multiple => true, :index => "bar" }, 3)
|
||||
)
|
||||
|
||||
end
|
||||
|
||||
def test_checkbox_disabled_disables_hidden_field
|
||||
assert_dom_equal(
|
||||
'<input name="post[secret]" type="hidden" value="0" disabled="disabled"/><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
|
||||
|
|
Loading…
Reference in a new issue