-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix error indexes #28064
fix error indexes #28064
Conversation
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
There's an existing pull request that does something similar to this: #24728 As I just mentioned there, I think the index is meant to point to the location of the error in the changes that were applied, not the records in the association. In the test you linked to demonstrate the problem, only one comment has changed, so I believe |
I got your point. but.. if so, then I wonder how do we effectively use this indexed In a case like this one, how do we use the |
If you're processing the errors in Rails, you can get them directly from the association: post.comments.each do |comment|
if comment.invalid?
# do something with comment.errors
end
end You don't need to look things up with an index; you already have the record. There isn't much documentation for |
If this is the main reason why the error messages have indexes now, then I suspect the current indexes are not useful to fulfill the issue #8638 because we still can't indicate which error message is for which child object. (index # mismatched) By the way, I am also using this method to return JSON data, not to build a HTML form. |
Okay, here's an example. I make a request to add two comments to a post: PATCH /posts/1 HTTP/1.0
{ "comments_attributes": [ { "body": "valid body" }, { "body": "" } ] } With the current implementation, if I serialize { "errors": { "comments[1].body": ["can't be blank"] } } I can use the error index to tell that the second comment was invalid. If we change the indexes to be based on the records in the association, and the post already has three comments, I'll get this instead: { "errors": { "comments[4].body": ["can't be blank"] } } How can I determine which one of the comments I tried to create was invalid? |
Closing in favor of #24728, as it looks to be closer to being mergeable. |
In my case I have a Rails + Vue.js app. When I save, I was counting on the indexes to tell me which table row has the error. This works great as long as all rows get validated, but in the case where only some rows are validated the indexes point to the "wrong" rows. I'm actually pushing all of the rows back to Rails via ajax. Rails decides which ones need to be saved/validated, so I'm not sure how I would decipher the indexes upon response. |
I didn't have enough info. to run
rails
tests, so I created a repo to test this issue.Summary
https://github.com/github0013/index_errors/blob/master/spec/models/post_spec.rb#L44-L45
A post has 2 comments.
A. When the first comment has errors, the index is correct (first == 0).
B. When the first comment is valid, but the second is invalid, the error index should be 1; however the index is 0.
Other Information
https://github.com/github0013/index_errors/blob/master/spec/models/post_spec.rb#L62-L66
this checks error child's correct index