# Problem with presentation of error messages and multiple objects

**URL:** <https://www.ruby-forum.com/t/problem-with-presentation-of-error-messages-and-multiple-objects/226034>\
**Category:** Rails\
**Created:** [February 21, 2009, 2:15am UTC](https://www.ruby-forum.com/t/problem-with-presentation-of-error-messages-and-multiple-objects/226034 "2009-02-21T02:15:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 21, 2009, 2:15am UTC](https://www.ruby-forum.com/t/problem-with-presentation-of-error-messages-and-multiple-objects/226034/1 "2009-02-21T02:15:50Z")

</div>

Hi!

I have a problem with the presentation of error messages for multiple  
objects. The validation works fine and if errors occure they will all  
be presented, but only for one object at a time. When I submit my form  
the occuring errors will be presented and if I fix them and submit the  
form again the errors for the next form will be presented and so on.

I don’t know what I have to do to show all errors for all objects at  
once.

In my view template I use \<%= error\_messages\_for payment,  
subscription, user %\>.

This is how my create method looks like:

def create  
@payment = Payment.new(params[:payment])  
@subscription = Subscription.new(params[:subscription])  
@user = User.new(params[:user])

@service = SubscriptionService.new(@subscription, @user, @payment)

if @service.save  
flash[:notice] = ‘Subscription was successfully created.’  
redirect\_to root\_url  
else  
render :action =\> “new”  
end  
end

This is how my SubscriptionService looks like:

class SubscriptionService

attr\_reader :payment, :subscription, :user

def initialize(subscription, user, payment)  
@payment = payment  
@user = user  
@subscription = subscription  
end

def save  
return false unless valid?  
begin  
Subscription.transaction do  
if @payment.new\_record?  
@payment.subscription = @subscription  
@payment.save!  
end  
if @user.new\_record?  
@user.subscription = @subscription  
@user.save!  
end  
@subscription.save!  
true  
end  
rescue  
false  
end  
end

def valid?  
@payment.valid? && @subscription.valid? && @user.valid?  
end  
end

I think this is pretty basic code and I already found out that the  
presentation has something to do with valid? method in the  
SubscriptionService but as I said I don’t know how and where to  
collect all error messages and present them at once.

Many thanks in advance.

Regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 22, 2009, 4:36am UTC](https://www.ruby-forum.com/t/problem-with-presentation-of-error-messages-and-multiple-objects/226034/2 "2009-02-22T04:36:30Z")

</div>

You haven`t showed your view code, so i`m assumming that every object  
has it`s own “place”, so why don’t you just add an error\_messages\_for  
every object at the beginning of their part in the form?

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 22, 2009, 8:30am UTC](https://www.ruby-forum.com/t/problem-with-presentation-of-error-messages-and-multiple-objects/226034/3 "2009-02-22T08:30:53Z")

</div>

No, it’s one form with which I update all of my models. The form  
itself belongs to the subscription model.

The subscription model belongs to the payment and user model and the  
payment and user model on the other site have one subscription.

I’m trying to get a clue how I can solve this but the only thing I  
found out by now is that I’m not the first one with this problem (how  
wondering :)) but no solution or at least something that points me in  
the right direction (ok, updating to Edge Rails is no solution for me  
right now).

Every hint is appreciated.

Regards,  
Stefan

On 22 Feb., 04:35, Maurício Linhares [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 23, 2009, 4:43pm UTC](https://www.ruby-forum.com/t/problem-with-presentation-of-error-messages-and-multiple-objects/226034/4 "2009-02-23T16:43:08Z")

</div>

For everyone running in something similar. In my case the problem is  
how I checked in my SubscriptionService that every model is valid:

def valid?  
@payment.valid? && @subscription.valid? && @user.valid?  
end

The logical and will only check the next operand if the one befor is  
true. So, for example, when the first validation fails the next two  
validations will not take place.

This is my solution:

def valid?  
valid = true  
valid = false unless @payment.valid?  
valid = false unless @subscription.valid?  
valid = false unless @user.valid?  
valid  
end

Perhaps it helps someone.

Regards, Stefan
