How to raise exception with save!

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.

‘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

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

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

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

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

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?

RSL

I thought that might be the case. :slight_smile: 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs