Skip to content

Commit 64aa9df

Browse files
committed
Add bootstrap_fields form helper.
1 parent 28def01 commit 64aa9df

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ in `form_with`.
164164

165165
`form_with` has some important differences compared to `form_for` and `form_tag`, and these differences apply to `bootstrap_form_with`. A good summary of the differences can be found at: https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a, or in the [Rails documentation](api.rubyonrails.org).
166166

167-
### bootstrap_fields_for
167+
### bootstrap_fields_for and bootstrap_fields
168168

169-
Adding fields for a different object without nesting can be achieved using the `bootstrap_fields_for` helper in the same way it is done in Rails.
169+
Adding fields for a different object without nesting can be achieved using the `bootstrap_fields_for` and
170+
`bootstrap_fields` helpers in the same way it is done in Rails.
170171

171172
![Example 3](demo/doc/screenshots/bootstrap/readme/03_example.png "Example 3")
172173
```erb
@@ -175,6 +176,9 @@ Adding fields for a different object without nesting can be achieved using the `
175176
<%= bootstrap_fields_for :parent do |pf| %>
176177
<%= pf.email_field :email, label: 'Parent email' %>
177178
<% end %>
179+
<%= bootstrap_fields @user.address do |af| %>
180+
<%= af.text_field :street_name %>
181+
<% end %>
178182
<%= f.primary "Save" %>
179183
<% end %>
180184
```
@@ -191,6 +195,10 @@ Generated HTML:
191195
<label class="form-label" for="parent_email">Parent email</label>
192196
<input class="form-control" id="parent_email" name="parent[email]" type="email">
193197
</div>
198+
<div class="mb-3">
199+
<label class="form-label" for="street_name">Street name</label>
200+
<input class="form-control" id="street_name" name="street_name" type="text">
201+
</div>
194202
<input class="btn btn-primary" data-disable-with="Save" name="commit" type="submit" value="Save">
195203
</form>
196204
```
2.72 KB
Loading

lib/bootstrap_form/action_view_extensions/form_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ def bootstrap_fields_for(record_name, record_object=nil, options={}, &block)
4444
fields_for(record_name, record_object, options, &block)
4545
end
4646

47+
def bootstrap_fields(scope=nil, model: nil, **options, &block)
48+
options[:builder] = BootstrapForm::FormBuilder
49+
fields(scope, model: model, **options, &block)
50+
end
51+
4752
private
4853

4954
def with_bootstrap_form_field_error_proc

test/bootstrap_fields_for_test.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BootstrapFieldsForTest < ActionView::TestCase
2626
assert_equivalent_html expected, output
2727
end
2828

29-
test "bootstrap_form_for helper works for serialized hash attributes" do
29+
test "bootstrap_fields_for helper works for serialized hash attributes" do
3030
@user.preferences = { "favorite_color" => "cerulean" }
3131

3232
output = bootstrap_form_for(@user) do |_f|
@@ -46,4 +46,46 @@ class BootstrapFieldsForTest < ActionView::TestCase
4646
HTML
4747
assert_equivalent_html expected, output
4848
end
49+
50+
test "bootstrap_fields helper works for associations" do
51+
@user.address = Address.new(street: "123 Main Street")
52+
53+
output = bootstrap_form_for(@user) do |_f|
54+
bootstrap_fields @user.address do |af|
55+
af.text_field(:street)
56+
end
57+
end
58+
59+
expected = <<~HTML
60+
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
61+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
62+
<div class="mb-3">
63+
<label class="form-label" for="address_street">Street</label>
64+
<input class="form-control" id="address_street" name="address[street]" type="text" value="123 Main Street" />
65+
</div>
66+
</form>
67+
HTML
68+
assert_equivalent_html expected, output
69+
end
70+
71+
test "bootstrap_fields helper works for serialized hash attributes" do
72+
@user.preferences = { "favorite_color" => "cerulean" }
73+
74+
output = bootstrap_form_for(@user) do |_f|
75+
bootstrap_fields :preferences do |builder|
76+
builder.text_field :favorite_color, value: @user.preferences["favorite_color"]
77+
end
78+
end
79+
80+
expected = <<~HTML
81+
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
82+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
83+
<div class="mb-3">
84+
<label class="form-label" for="preferences_favorite_color">Favorite color</label>
85+
<input class="form-control" id="preferences_favorite_color" name="preferences[favorite_color]" type="text" value="cerulean" />
86+
</div>
87+
</form>
88+
HTML
89+
assert_equivalent_html expected, output
90+
end
4991
end

0 commit comments

Comments
 (0)