Skip to content

Commit 8d6550c

Browse files
committed
Don't raise exception when no model
1 parent d2862ad commit 8d6550c

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/bootstrap_form/form_group_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def form_group_css_options(method, html_options, options)
107107
end
108108

109109
def form_group_placeholder(options, method)
110-
form_group_label_text(options[:label]) || object.class.human_attribute_name(method)
110+
form_group_label_text(options[:label]) || (object && object.class.human_attribute_name(method)) || method.to_s.humanize # rubocop:disable Style/SafeNavigation
111111
end
112112
end
113113
end

test/bootstrap_form_group_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ class BootstrapFormGroupTest < ActionView::TestCase
118118
assert_equivalent_html expected, @builder.text_field(:email, label_as_placeholder: true)
119119
end
120120

121+
test "label as placeholder with form_with" do
122+
expected = <<~HTML
123+
<div class="mb-3">
124+
<label class="visually-hidden required" for="user_email">Email</label>
125+
<input required="required" class="form-control" id="user_email" placeholder="Email" name="user[email]" type="text" value="steve@example.com" />
126+
</div>
127+
HTML
128+
assert_equivalent_html expected, form_with_builder.text_field(:email, label_as_placeholder: true, required: true)
129+
end
130+
121131
test "adding prepend text" do
122132
expected = <<~HTML
123133
<div class="mb-3">

test/test_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def setup_test_fixture
4141
end
4242

4343
# Originally only used in one test file but placed here in case it's needed in others in the future.
44+
# This simulartes `form_with` without a model, which tests aspects that wouldn't otherwise
45+
# be tested.
4446
def form_with_builder
45-
builder = nil
46-
bootstrap_form_with(model: @user) { |f| builder = f }
47-
builder
47+
BootstrapForm::FormBuilder.new(:user, false, self, {})
4848
end
4949

5050
def sort_attributes(doc)

0 commit comments

Comments
 (0)