Displaying errors for associated objects


#1

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


#2

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.


#3

Pat M. 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.