Skip to content

Commit 84b8537

Browse files
committed
Handle boolean options in fields_for
1 parent f947b14 commit 84b8537

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

demo/app/models/address.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class Address < ApplicationRecord
22
belongs_to :user
3+
4+
validates :city, presence: true
35
end

lib/bootstrap_form/form_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def fields_for_options(record_object, fields_options)
9090
field_options = fields_options
9191
field_options = record_object if record_object.is_a?(Hash) && record_object.extractable_options?
9292
%i[layout control_col inline_errors label_errors].each do |option|
93-
field_options[option] ||= options[option]
93+
field_options[option] = field_options.key?(option) ? field_options[option] : options[option]
9494
end
9595
field_options[:label_col] = field_options[:label_col].present? ? field_options[:label_col].to_s : options[:label_col]
9696
field_options

test/bootstrap_fields_for_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ class BootstrapFieldsForTest < ActionView::TestCase
2525
assert_equivalent_html expected, output
2626
end
2727

28+
test "bootstrap_fields_for helper works with boolean options" do
29+
@user.address = Address.new
30+
@user.valid?
31+
32+
output = bootstrap_form_for(@user, inline_errors: true) do |_f|
33+
bootstrap_fields_for :address, @user.address, inline_errors: false do |af|
34+
af.text_field(:city)
35+
end
36+
end
37+
38+
expected = <<~HTML
39+
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
40+
<div class="mb-3">
41+
<label class="form-label required" for="address_city">City</label>
42+
<input class="form-control is-invalid" id="address_city" name="address[city]" type="text" required="required" />
43+
<!-- No `<div class="invalid-feedback">can't be blank</div>` -->
44+
</div>
45+
</form>
46+
HTML
47+
assert_equivalent_html expected, output
48+
end
49+
2850
test "bootstrap_fields_for helper works for serialized hash attributes" do
2951
@user.preferences = { "favorite_color" => "cerulean" }
3052

0 commit comments

Comments
 (0)