Skip to content

Commit 00d072a

Browse files
authored
Merge pull request #764 from pienkowb/handle-boolean-options-in-fields-for
Properly handle boolean options in the `fields_for` helper
2 parents 09d09d6 + 64023bf commit 00d072a

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
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

demo/app/models/model_user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class ModelUser
22
include ActiveModel::Model
3+
34
attr_accessor :email, :password, :comments, :misc
45

56
validates :email, :password, presence: true

gemfiles/8.0.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ eval File.read(gems), binding, gems # rubocop: disable Security/Eval
44
gem "bigdecimal" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
55
gem "drb" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
66
gem "mutex_m" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
7-
gem "rails", "~> 8.0.1"
87
gem "propshaft"
8+
gem "rails", "~> 8.0.1"
99
gem "sqlite3"

gemfiles/edge.gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ eval File.read(gems), binding, gems # rubocop: disable Security/Eval
44
gem "bigdecimal" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
55
gem "drb" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
66
gem "mutex_m" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0")
7-
gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
87
gem "propshaft"
8+
gem "rails", git: "https://github.com/rails/rails.git", branch: "main"
99
gem "sqlite3"

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)