Skip to content

Commit 28def01

Browse files
committed
Add bootstrap_fields_for form helper.
1 parent 3867a40 commit 28def01

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/bootstrap_form/action_view_extensions/form_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def bootstrap_form_tag(options={}, &block)
3939
bootstrap_form_for("", options, &block)
4040
end
4141

42-
def bootstrap_fields_for(record_name, record_object = nil, options = {}, &block)
42+
def bootstrap_fields_for(record_name, record_object=nil, options={}, &block)
4343
options[:builder] = BootstrapForm::FormBuilder
4444
fields_for(record_name, record_object, options, &block)
4545
end

test/bootstrap_fields_for_test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require_relative "test_helper"
2+
3+
class BootstrapFieldsForTest < ActionView::TestCase
4+
include BootstrapForm::ActionViewExtensions::FormHelper
5+
6+
setup :setup_test_fixture
7+
8+
test "bootstrap_fields_for helper works for associations" do
9+
@user.address = Address.new(street: "123 Main Street")
10+
11+
output = bootstrap_form_for(@user) do |_f|
12+
bootstrap_fields_for @user.address do |af|
13+
af.text_field(:street)
14+
end
15+
end
16+
17+
expected = <<~HTML
18+
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
19+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
20+
<div class="mb-3">
21+
<label class="form-label" for="address_street">Street</label>
22+
<input class="form-control" id="address_street" name="address[street]" type="text" value="123 Main Street" />
23+
</div>
24+
</form>
25+
HTML
26+
assert_equivalent_html expected, output
27+
end
28+
29+
test "bootstrap_form_for helper works for serialized hash attributes" do
30+
@user.preferences = { "favorite_color" => "cerulean" }
31+
32+
output = bootstrap_form_for(@user) do |_f|
33+
bootstrap_fields_for :preferences do |builder|
34+
builder.text_field :favorite_color, value: @user.preferences["favorite_color"]
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+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
41+
<div class="mb-3">
42+
<label class="form-label" for="preferences_favorite_color">Favorite color</label>
43+
<input class="form-control" id="preferences_favorite_color" name="preferences[favorite_color]" type="text" value="cerulean" />
44+
</div>
45+
</form>
46+
HTML
47+
assert_equivalent_html expected, output
48+
end
49+
end

0 commit comments

Comments
 (0)