# Handle exception in ajax call

**URL:** <https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523>\
**Category:** Rails\
**Created:** [August 1, 2010, 5:56am UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523 "2010-08-01T05:56:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 1, 2010, 5:56am UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/1 "2010-08-01T05:56:35Z")

</div>

sorry to bother to help me again.I invoke create action using ajax.The  
variant and it’s associations are put in a transaction.When both are  
saved successfully,render create\_variant using ajax.When anyone  
unsuccessfully,rallback all the saving manipulation,rescue exception,and  
render the new\_variant using ajax,which has \<%=error\_messages\_for  
:variant-%\> to display invalidations.  
But when inputtings are invalid,i always get something like  
attachment.It seems the exception is not handled at all,the error  
meassages are not displayed as expected.can you help me? thank!  
###############  
def create  
@product=Product.find\_by\_id(params[:product\_id])  
@variant=Variant.new(params[:variant])  
@product.variants\<\<@variant  
@base\_option=Option.new(:name=\>params[:base\_option][:name],:price=\>0)  
@variant.options\<\<@base\_option  
Variant.transaction do  
begin  
@variant.options.each{|option|option.save!}  
@variant.save!  
flash.now[:notice]=‘variant created successfully’  
render :action=\>:create\_variant,:layout=\>false  
rescue Exception  
raise  
render :action=\>:new\_variant,:layout=\>false  
end  
end  
end  
##############

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 1, 2010, 2:42pm UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/2 "2010-08-01T14:42:39Z")

</div>

hi  
can you help me?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 2, 2010, 5:12am UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/3 "2010-08-02T05:12:39Z")

</div>

I tested using non-ajax call,but the problem was same,the exception was  
not handled like the attachment and new\_variant template was not  
rendered as expected.The render should not be used in the rescue block?  
can you help me?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 2, 2010, 10:37am UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/4 "2010-08-02T10:37:33Z")

</div>

On Aug 2, 4:12 am, Guo Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tested using non-ajax call,but the problem was same,the exception was  
> not handled like the attachment and new\_variant template was not  
> rendered as expected.The render should not be used in the rescue block?  
> can you help me?

You’re rescuing the exception but then you’re re-raising it so your  
render call never happens. Also a more usual way is to call save and  
test the return value of that rather than call save! and rescue the  
exception

Fred

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 2, 2010, 2:41pm UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/5 "2010-08-02T14:41:40Z")

</div>

On Aug 2, 1:11 pm, Guo Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > You’re rescuing the exception but then you’re re-raising it so your  
> > render call never happens. Also a more usual way is to call save and  
> > test the return value of that rather than call save! and rescue the  
> > exception
> 
> Thanks fred.But i want transaction(which need save!) to let them all  
> either to be saved or not.If i rescue the exception and not re-raising  
> it,is it right to handle exception in this way?

in that case you should rescue the exception outside of the  
transaction

Fred

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 2, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/6 "2010-08-02T17:19:55Z")

</div>

Thank you for your patience,fred.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 2, 2010, 2:11pm UTC](https://www.ruby-forum.com/t/handle-exception-in-ajax-call/192523/7 "2010-08-02T14:11:25Z")

</div>

> You’re rescuing the exception but then you’re re-raising it so your  
> render call never happens. Also a more usual way is to call save and  
> test the return value of that rather than call save! and rescue the  
> exception

Thanks fred.But i want transaction(which need save!) to let them all  
either to be saved or not.If i rescue the exception and not re-raising  
it,is it right to handle exception in this way?
