Forum: Ruby on Rails How do you display validation errors when validating child m

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf9e21cabb1af219d025d8642ae5ec27?d=identicon&s=25 Bryce Thornton (Guest)
on 2006-03-11 22:01
(Received via mailing list)
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
92dafea96df3ff949b426b235a19ae0e?d=identicon&s=25 ChrisT (Guest)
on 2006-03-17 12:44
(Received via mailing list)
Did you ever get this sorted? I am struggling with a similar situation,
and feeling particularly brain-dead at the moment.
Chris T
925c5035ff3e0005e0604c0cd869b5cf?d=identicon&s=25 Chris T (Guest)
on 2006-03-17 15:20
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
This topic is locked and can not be replied to.