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 ?
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 H. 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
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 H. 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
You 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
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 ?
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 N. 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?
I thought that might be the case. I’d go with the rescue exception
approach then. The API [ ActiveRecord::Transactions::ClassMethods]
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.