leastbad / optimism

The missing drop-in solution for realtime remote form validation in Rails.
MIT License
361 stars 42 forks source link

Handle fields_for on has_one relationships #10

Closed joshleblanc closed 4 years ago

joshleblanc commented 4 years ago

The existing code assumes that if you have a fields ending with _attributes, that it's a collection. This isn't neccessarily true. You can have a nested one-to-one relationship, where x_attributes is just a object, rather than a collection.

These changes account for that fact.

Specifically, line 60 checks if the association can be iterated over, otherwise process it as a resource.

Line 76 handles ancestry only having 2 elements. Previous if Foo belongs to Bar, then the ancestory would be ['foo', 'bar']. This would create a resource name of foo_bar_attributes_, which has a trailing underscore.

leastbad commented 4 years ago

@joshleblanc, you are a true gem.

Thanks for catching this.