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:
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.