From 8d6550c39f67a2a29e76c73ebe424f5dab8fb87c Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Tue, 20 Jan 2026 02:27:57 +0000 Subject: [PATCH] Don't raise exception when no model --- lib/bootstrap_form/form_group_builder.rb | 2 +- test/bootstrap_form_group_test.rb | 10 ++++++++++ test/test_helper.rb | 6 +++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/bootstrap_form/form_group_builder.rb b/lib/bootstrap_form/form_group_builder.rb index 620433da..94ec6dab 100644 --- a/lib/bootstrap_form/form_group_builder.rb +++ b/lib/bootstrap_form/form_group_builder.rb @@ -107,7 +107,7 @@ def form_group_css_options(method, html_options, options) end def form_group_placeholder(options, method) - form_group_label_text(options[:label]) || object.class.human_attribute_name(method) + form_group_label_text(options[:label]) || (object && object.class.human_attribute_name(method)) || method.to_s.humanize # rubocop:disable Style/SafeNavigation end end end diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index 5f15035e..acc9d102 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -118,6 +118,16 @@ class BootstrapFormGroupTest < ActionView::TestCase assert_equivalent_html expected, @builder.text_field(:email, label_as_placeholder: true) end + test "label as placeholder with form_with" do + expected = <<~HTML +
+ + +
+ HTML + assert_equivalent_html expected, form_with_builder.text_field(:email, label_as_placeholder: true, required: true) + end + test "adding prepend text" do expected = <<~HTML
diff --git a/test/test_helper.rb b/test/test_helper.rb index 83fee89e..da0547aa 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -41,10 +41,10 @@ def setup_test_fixture end # Originally only used in one test file but placed here in case it's needed in others in the future. + # This simulartes `form_with` without a model, which tests aspects that wouldn't otherwise + # be tested. def form_with_builder - builder = nil - bootstrap_form_with(model: @user) { |f| builder = f } - builder + BootstrapForm::FormBuilder.new(:user, false, self, {}) end def sort_attributes(doc)