Hi, I am using transaction as : Merchant.transaction do @merchant.save! end Now, how can I trap the exception and show in a user friendly way if the save fails. I need to show signup again if save fails, and go to ''profile'' if save is a success. How can I do that ? Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> Hi, > > I am using transaction as : > > Merchant.transaction do > @merchant.save! > end > > Now, how can I trap the exception and show in a user friendly way if the > save fails. I need to show signup again if save fails, and go to > ''profile'' if save is a success. How can I do that ?rescue that exception... something like: begin Merchant.transaction do @merchant.save! end rescue WhateverTheExceptionThrownNameIsHere redirect_to ''/doh_an_error'' # or set flash[:error] or a add an error or something else end --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
But how do I know the name of the error? There can be more than one type of error.How can say that whatever kind of error occurs, go to signup? Similar to java catch Exception, how can I do here? Philip Hallstrom wrote:>> ''profile'' if save is a success. How can I do that ? > rescue that exception... > > something like: > > begin > Merchant.transaction do > @merchant.save! > end > rescue WhateverTheExceptionThrownNameIsHere > redirect_to ''/doh_an_error'' > # or set flash[:error] or a add an error or something else > end-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rm Rm wrote:> But how do I know the name of the error? There can be more than one type > of error.How can say that whatever kind of error occurs, go to signup? > Similar to java catch Exception, how can I do here? > > Philip Hallstrom wrote: >>> ''profile'' if save is a success. How can I do that ? >> rescue that exception... >> >> something like: >> >> begin >> Merchant.transaction do >> @merchant.save! >> end >> rescue WhateverTheExceptionThrownNameIsHere >> redirect_to ''/doh_an_error'' >> # or set flash[:error] or a add an error or something else >> endYou can leave off the exception name and it will catch any error. Or if you do want to examine the error in any way you can do begin ... rescue Exception => e raise e unless e.kind_of?(FooError) end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''m a noob but I checked http://api.rubyonrails.org/ exceptions and ActiveRecordError generic error class and superclass of all other errors raised by Active Record might help you But looking at the code for save (create_or_update), it doesn;t seem to raise any exceptions. Hope this helps Rm Rm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: Hi, I am using transaction as : Merchant.transaction do @merchant.save! end Now, how can I trap the exception and show in a user friendly way if the save fails. I need to show signup again if save fails, and go to ''profile'' if save is a success. How can I do that ? Thanks. -- Posted via http://www.ruby-forum.com/. --------------------------------- What kind of emailer are you? Find out today - get a free analysis of your email personality. Take the quiz at the Yahoo! Mail Championship. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Excuse my ignorance but why are you using a transaction here for one save when if @merchant.save redirect_to :action => :whatever else # Stay here, render your error, and let the user correct the form end sounds like it''d work just perfectly for what you need? RSL On 2/16/07, Rm Rm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi, > > I am using transaction as : > > Merchant.transaction do > @merchant.save! > end > > Now, how can I trap the exception and show in a user friendly way if the > save fails. I need to show signup again if save fails, and go to > ''profile'' if save is a success. How can I do that ? > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I just gave an example with one save, wheras in transaction I am supposed to save and update related records in about 6-7 tables. Hence , I do need transaction. But it seems that to display all the possible exceptions and to display user friendly messages for each of them, I have to put code in rescue. Is there a way to nest transaction within a transaction? Russell Norris wrote:> Excuse my ignorance but why are you using a transaction here for one > save > when > > if @merchant.save > redirect_to :action => :whatever > else > # Stay here, render your error, and let the user correct the form > end > > sounds like it''d work just perfectly for what you need? > > RSL-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I thought that might be the case. :) I''d go with the rescue exception approach then. The API [ http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html] seems to suggest that the transaction will raise an exception that you can rescue. You big hero, you. If you''re trying to determine which of the object.saves failed you could always do something like begin saved = [] Merchant.transaction do |t| @merchant.save! saved << :merchant @other_thing.save! saved << :other_thing end # Make sure to redirect before the end of the error handling redirect_to :action => :whatever rescue # Stay here, render the error, let the user correct and resubmit the form # Check the "saved" variable to see which models got saved correctly. end And, yes, you can nest transactions as well. RSL On 2/17/07, Rm Rm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I just gave an example with one save, wheras in transaction I am > supposed to save and update related records in about 6-7 tables. Hence , > I do need transaction. But it seems that to display all the possible > exceptions and to display user friendly messages for each of them, I > have to put code in rescue. Is there a way to nest transaction within a > transaction? > > Russell Norris wrote: > > Excuse my ignorance but why are you using a transaction here for one > > save > > when > > > > if @merchant.save > > redirect_to :action => :whatever > > else > > # Stay here, render your error, and let the user correct the form > > end > > > > sounds like it''d work just perfectly for what you need? > > > > RSL > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---