Forum: Ruby on Rails Displaying errors for associated objects

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.
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2005-12-01 17:14
(Received via mailing list)
I really like using error_messages_for in my views.  In one of my
views, I create a ticket object and a message object, and the message
belongs to the ticket.  When I do error_messages_for(:ticket), I'd
like to get any errors on the associated message.  Something like:

Two errors prevented this ticket from being saved.
There were problems with the following fields:
Subject can't be blank    (this is a Ticket attribute)
Body can't be blank        (this is a Message attribute)

A Ticket has_many :messages, so right now if the body is blank, I get
a message that says "Messages is invalid."  I was wondering if somehow
I could display the exact error message for the Message in the same
box as the error for the Ticket.

Thanks,
Pat
7fb3901cce24ff7f9bd517ea92c898ae?d=identicon&s=25 Nathan Ventura (pie-social)
on 2006-05-07 02:09
I really wish this was easy to do, I've never gotten it to work. Any
thoughts anybody? Please follow up as I would love to know the solution.
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-05-07 02:42
Pat Maddox wrote:
> I really like using error_messages_for in my views.  In one of my
> views, I create a ticket object and a message object, and the message
> belongs to the ticket.  When I do error_messages_for(:ticket), I'd
> like to get any errors on the associated message.  Something like:
>
> Two errors prevented this ticket from being saved.
> There were problems with the following fields:
> Subject can't be blank    (this is a Ticket attribute)
> Body can't be blank        (this is a Message attribute)
>
> A Ticket has_many :messages, so right now if the body is blank, I get
> a message that says "Messages is invalid."  I was wondering if somehow
> I could display the exact error message for the Message in the same
> box as the error for the Ticket.
>
> Thanks,
> Pat

A long shot, but check out

ticket.messages.last.errors

the message that was just added should still be there, just unasved due
to a validation error.  In theory anyway.
This topic is locked and can not be replied to.