Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Conversation

github0013
Copy link

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

@rails-bot
Copy link

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.

@eugeneius
Copy link
Member

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 {:"comments[0].body"=>"can't be blank"} is correct.

@github0013
Copy link
Author

I got your point. but.. if so, then I wonder how do we effectively use this indexed Parent.errors? as the other developers are also confused of its usage.

In a case like this one, how do we use the [0] index for the second child error?

@eugeneius
Copy link
Member

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 index_errors, but based on the pull request that originally proposed the feature (#8638) it looks like it's primarily intended to be used when the errors will be sent back to the client.

@github0013
Copy link
Author

#8638

See that it is impossible to detect whichEmployment object is error related to.

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.

@eugeneius
Copy link
Member

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 post.errors.messages I'll get the following:

{ "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?

@sgrif
Copy link
Contributor

sgrif commented Jul 17, 2017

Closing in favor of #24728, as it looks to be closer to being mergeable.

@sgrif sgrif closed this Jul 17, 2017
@github0013 github0013 deleted the fix_index_errors branch July 20, 2017 05:26
@dwieringa
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants