Forum: Ruby on Rails Handling nil.errors in error_messages_for

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.
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2008-10-15 23:59
How do you deal with multiple objects in error_messages_for when one or
more may be nil?

For example:

<%= error_messages_for :object => [@account, @account.billing_detail,
@account.mailing_address, @account.billing_address] %>

Here, @account.billing_detail may be nil, and throw a NoMethodError.

Thanks,
Mark
Beb127032d16730a7f6247b809142ec6?d=identicon&s=25 command0 (Guest)
on 2008-10-16 03:18
(Received via mailing list)
Now, is @account a variable or a class? I could be way off in left
field, but it looks like your calling objects that should be from a
class. Variables should only be called by themselves typically.

On Oct 15, 3:59 pm, Mark Richman <rails-mailing-l...@andreas-s.net>
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2008-10-16 04:36
@account is an instance of class Account.

Account has_one BillingDetail
Account has_one MailingAddress
Account has_one BillingAddress

My issue is how to deal with nil references on the has_one associations.
To my knowledge, the above syntax is the only way to deal with error
messages in a multi-model form. I could have also done this:

<%= error_messages_for :object => [@account, @billing_detail,
@mailing_address, @billing_address] %>

having set these class variables in the controller, but that does not
solve the nil issue.

Thanks,
Mark
1b507a552850441a516948d488962d2e?d=identicon&s=25 Sniper Abandon (mokkai)
on 2008-10-16 06:27
Mark Richman wrote:
> How do you deal with multiple objects in error_messages_for when one or
> more may be nil?
>
> For example:
>
> <%= error_messages_for :object => [@account, @account.billing_detail,
> @account.mailing_address, @account.billing_address] %>
>
> Here, @account.billing_detail may be nil, and throw a NoMethodError.
>
> Thanks,
> Mark

i don't know why you are trying in this way

anyway try this
<%= error_messages_for :object => [@account, @account.billing_detail,
@account.mailing_address, @account.billing_address].compact %>

by
mokkai
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2008-10-16 12:47
That worked! Thank you...had no idea you could do that...guess I should
learn more Ruby :)

> anyway try this
> <%= error_messages_for :object => [@account, @account.billing_detail,
> @account.mailing_address, @account.billing_address].compact %>
>
> by
> mokkai
1b507a552850441a516948d488962d2e?d=identicon&s=25 Sniper Abandon (mokkai)
on 2008-10-16 13:06
Mark Richman wrote:
>had no idea you could do that...
why ?
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2008-10-16 13:07
Sniper Abandon wrote:
> Mark Richman wrote:
>>had no idea you could do that...
> why ?

Why did I have no idea??
This topic is locked and can not be replied to.