Skip to content

Commit 8ff07f8

Browse files
committed
Added fields_for example with inline errors to the README.md
Fixes #656
1 parent 64aa9df commit 8ff07f8

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,9 @@ error will be displayed below the field. Here's an example:
14231423
```erb
14241424
<%= bootstrap_form_for(@user_with_error) do |f| %>
14251425
<%= f.email_field :email %>
1426+
<%= f.fields_for :address do |af| %>
1427+
<%= af.text_field :street %>
1428+
<% end %>
14261429
<% end %>
14271430
```
14281431

@@ -1435,6 +1438,11 @@ Generated HTML:
14351438
<input class="form-control is-invalid" id="user_email" name="user[email]" required="required" type="email" value="steve.example.com">
14361439
<div class="invalid-feedback">is invalid</div>
14371440
</div>
1441+
<div class="mb-3">
1442+
<label class="form-label" for="user_address_attributes_street">Street</label>
1443+
<input class="form-control is-invalid" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="Bar">
1444+
<div class="invalid-feedback">is invalid</div>
1445+
</div>
14381446
</form>
14391447
```
14401448

demo/app/controllers/bootstrap_controller.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ def fragment
1616

1717
def load_models
1818
@address = Address.new(id: 1, street: "Foo")
19-
@collection = [
20-
@address,
21-
Address.new(id: 2, street: "Bar")
22-
]
23-
19+
@address_with_error = Address.new(id: 2, street: "Bar")
20+
@address_with_error.errors.add(:street)
21+
@collection = [@address, @address_with_error]
2422
@user = User.new email: "[email protected]"
25-
26-
@user_with_error = User.new email: "steve.example.com"
23+
@user_with_error = User.new email: "steve.example.com", address: @address_with_error
2724
@user_with_error.errors.add(:email)
2825
@user_with_error.errors.add(:misc)
29-
3026
@users = [@user]
3127
end
3228
end
4.32 KB
Loading

0 commit comments

Comments
 (0)