Skip to content

Commit f42dbae

Browse files
committed
Add instructions for integrating with Cocoon to README.
1 parent 43f38d9 commit f42dbae

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,26 @@ customer.addresses # => [#<AddressValue:0x00ba9876543210 @city="Micropolis">]
164164

165165
This is useful when data is submitted via standard HTML forms encoded with the 'application/x-www-form-urlencoded' media type (which cannot represent empty collections). To work around this, a dummy item can be added to the collection with it's key set to '-1' and it will conveniently be ignored when assigned to the value object collection.
166166

167+
### Integrate with Cocoon
168+
169+
Put this into a Rails initializer (e.g. `config/initializers/value_objects.rb`):
170+
171+
```ruby
172+
ValueObjects::ActionView.integrate_with :cocoon
173+
```
174+
175+
This will add the `link_to_add_nested_value` & `link_to_remove_nested_value` view helpers.
176+
Use them in place of Cocoon's `link_to_add_association` & `link_to_remove_association` when working with nested value objects:
177+
178+
```ruby
179+
# use the attribute name (:addresses) in place of the association name
180+
# and supply the value object class as the next argument
181+
link_to_add_nested_value 'Add Address', f, :addresses, AddressValue
182+
183+
# the `f` form builder argument is not needed
184+
link_to_remove_nested_value 'Remove Address'
185+
```
186+
167187
## Maintainers
168188

169189
* Matthew Yeow (https://github.com/tbsmatt), Tinkerbox Studios (https://www.tinkerbox.com.sg/)

0 commit comments

Comments
 (0)