Stefan Frede
2009-Feb-21 01:15 UTC
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Feb-22 03:35 UTC
Re: Problem with presentation of error messages and multiple objects
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/ (pt-br) | http://blog.codevader.com/ (en) On Fri, Feb 20, 2009 at 10:15 PM, Stefan Frede <sfrede-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stefan Frede
2009-Feb-22 07:30 UTC
Re: Problem with presentation of error messages and multiple objects
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 <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > On Fri, Feb 20, 2009 at 10:15 PM, Stefan Frede <sfr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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? > > -MeYWBit5NRH3/JtdY0R+dZlDKeVEtLwC@public.gmane.org = @subscription > > -MeYWBit5NRFLYScDn9snng@public.gmane.org! > > end > > if @user.new_record? > > -EEvP6i2/dEia1jiPU1X/t/8+0UxHXcjY@public.gmane.org = @subscription > > -uQpvtAwYA0+zQB+pC5nmwQ@public.gmane.org! > > end > > -9/ybXWNEfnUYObqrr1GBirNAH6kLmebB@public.gmane.org! > > true > > end > > rescue > > false > > end > > end > > > def valid? > > -MeYWBit5NRGAYAbWAYxApQ@public.gmane.org? && @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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Stefan Frede
2009-Feb-23 15:42 UTC
Re: Problem with presentation of error messages and multiple objects
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 On 22 Feb., 08:30, Stefan Frede <sfr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > 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 Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > > On Fri, Feb 20, 2009 at 10:15 PM, Stefan Frede <sfr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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? > > > -MeYWBit5NRH3/JtdY0R+dZlDKeVEtLwC@public.gmane.org = @subscription > > > -MeYWBit5NRFLYScDn9snng@public.gmane.org! > > > end > > > if @user.new_record? > > > -EEvP6i2/dEia1jiPU1X/t/8+0UxHXcjY@public.gmane.org = @subscription > > > -uQpvtAwYA0+zQB+pC5nmwQ@public.gmane.org! > > > end > > > -9/ybXWNEfnUYObqrr1GBirNAH6kLmebB@public.gmane.org! > > > true > > > end > > > rescue > > > false > > > end > > > end > > > > def valid? > > > -MeYWBit5NRGAYAbWAYxApQ@public.gmane.org? && @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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---