Problem with presentation of error messages and multiple objects

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

You havent showed your view code, so im 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/ (pt-br) | http://blog.codevader.com/
(en)

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 [email protected]

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