How do you display validation errors when validating child m


#1

I can’t seem to figure out how to get child validation error messages
to display. I’m going through the simple blog example DHH uses in his
video. In the example, a comment belongs_to a post. Validating posts
works fine and the errors are displayed as they should be. When I try
to validate a comment using:

validates_presence_of :summary

It does the proper validation by not inserting the comment, but the
validation error message isn’t displayed when the controller
redirects_to the ‘show’ action. So, any ideas about how to get these
child validations to display properly in the parent’s view?

Thanks,
Bryce


#2

Did you ever get this sorted? I am struggling with a similar situation,
and feeling particularly brain-dead at the moment.
Chris T


#3

Bryce Thornton wrote:

I can’t seem to figure out how to get child validation error messages
to display. I’m going through the simple blog example DHH uses in his
video. In the example, a comment belongs_to a post. Validating posts
works fine and the errors are displayed as they should be. When I try
to validate a comment using:

validates_presence_of :summary

Thanks,
Bryce

OK, with the help of an another thread got this sorted, so if you’re
still struggling this is how I did it (though there’s prob better ways).

The problem (I think) is that I was svaing the parent-child relationship
and with it the child. The child failed the validation but (and here I’m
guessing) the error message was lost by the parent.save method. So I
simply encased the parent.child save with an if child.valid? statement.
Which threw me back to the form with the error messages and fields as
before.

Except…[and I’m sure I saw the solution to this somewhere but can’t
for the life of me find it now] the parent.id that was originally in the
URL when the form was first visited (from a link: create a new child for
this item), and was submitted with the other form items as a hidden
field, is no longer there, and I can’t for the life of me think of an
elegant way of keepiong in the form. Any ideas?

Chris T