Deeply nested attributes#697
Merged
tagliala merged 2 commits intoDavyJonesLocker:masterfrom Mar 7, 2017
Merged
Conversation
Consider the following example:
class User < ActiveRecord::Base
has_many :phone_numbers
accepts_nested_attributes_for :phone_numbers
end
class PhoneNumber < ActiveRecord::Base
# This is a bit contrived, because probably it should be
# many-to-many
has_many :labels
accepts_nested_attributes_for :labels
end
class Label < ActiveRecord::Base
belongs_to :phone_number
end
If you render this on 1 page to edit labels you end up with the
following attributes name
user[phone_numbers_attributes][0][labels_attributes][0][label]
And validation is on field
user[phone_numbers_attributes][][label_attributes][][label]
At the moment the most current version of client side validation only
supports 1 level of nesting of accepts_nested_attributes_for. This
commit makes it support any level of nesting.
Contributor
|
@libc 👏 👏 👏 |
Contributor
|
@libc please give the master branch a try, I will then release a new version of the gem |
Contributor
Author
|
We're still using I tried master on our experimental |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Consider the following example:
If you render this on 1 page to edit labels you end up with the following attributes name:
Validation on this field will be generated automatically as:
The current nested attributes javascript looks like
/\[(\w+_attributes)\].*\[(\w+)\]$/, which doesn't properly process this nested attributes, with this PR it'll be supported.There's one use-case that is covered by the current deeply-nested validation, that I do not cover.
user[phone_numbers_attributes][deeply][nested][][attribute]searches for/\[phone_numbers_attributes\].*\[\]\[attribute\]$/in the validators. So it can match something unintended. I left it as it is right now and only processI left the initial commit of my colleague @Arie in.
Any feedback welcome.