Skip to content

Commit 3592d36

Browse files
committed
Backport field_name method for Rails 6.1.
1 parent 919ac57 commit 3592d36

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/bootstrap_form/inputs/collection_check_boxes.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def collection_check_boxes_with_bootstrap(*args)
2121
end
2222

2323
bootstrap_alias :collection_check_boxes
24+
25+
if Rails::VERSION::MAJOR < 7
26+
def field_name(method, *methods, multiple: false, index: @options[:index])
27+
object_name = @options.fetch(:as) { @object_name }
28+
29+
@template.field_name(object_name, method, *methods, index: index, multiple: multiple)
30+
end
31+
end
2432
end
2533
end
2634
end

test/bootstrap_checkbox_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,19 @@ class BootstrapCheckboxTest < ActionView::TestCase
473473
:street, checked: collection)
474474
end
475475

476+
if Rails::VERSION::MAJOR < 7
477+
def field_name(object_name, method_name, *method_names, multiple: false, index: nil)
478+
names = method_names.map! { |name| "[#{name}]" }.join
479+
if object_name.blank?
480+
"#{method_name}#{names}#{multiple ? '[]' : ''}"
481+
elsif index
482+
"#{object_name}[#{index}][#{method_name}]#{names}#{multiple ? '[]' : ''}"
483+
else
484+
"#{object_name}[#{method_name}]#{names}#{multiple ? '[]' : ''}"
485+
end
486+
end
487+
end
488+
476489
test "collection_check_boxes renders with include_hidden options correctly" do
477490
collection = [Address.new(id: 1, street: "Foo"), Address.new(id: 2, street: "Bar")]
478491
expected = <<~HTML

0 commit comments

Comments
 (0)