# Ajax post raise error

**URL:** <https://www.ruby-forum.com/t/ajax-post-raise-error/125536>\
**Category:** Rails\
**Created:** [January 9, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/ajax-post-raise-error/125536 "2008-01-09T23:55:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [January 9, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/ajax-post-raise-error/125536/1 "2008-01-09T23:55:40Z")

</div>

Hi,

within my rails application i have a view which does an ajax call for  
saving data. what i’d like to do is raise an exception within my  
controller that causes the onFailure event to be triggered. for  
example

begin  
Orderitem.destroy(params[:id])  
render :text =\> “order item deleted”  
rescue Exception =\> e  
flash[:notice] = e.message  
end

when an exception is captured i.e the rescue exception, how can i  
raise an exception; in other langages/platforms i’d construct a 406  
(if i remember correctly) response and write a nice message which  
would then be captured by the ajax call and prompt the user.

any help would be greatly appreciated.

thanks

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [January 10, 2008, 12:00am UTC](https://www.ruby-forum.com/t/ajax-post-raise-error/125536/2 "2008-01-10T00:00:57Z")

</div>

It is not wise to rescue just Exception, try to rescue something more  
specific, like an ActiveRecord::RecordNotFound or something.

The reason for this is because if something else errors in your code,  
you’ll  
still have it rescuing the exception and throwing a false error message.

On Jan 10, 2008 9:25 AM, hiddenhippo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> render :text => "order item deleted"
> 
> ```
> 
> thanks
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![hiddenhippo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/65b543/32.png) [@hiddenhippo](https://www.ruby-forum.com/u/hiddenhippo)\
**Post date:** [January 12, 2008, 11:20am UTC](https://www.ruby-forum.com/t/ajax-post-raise-error/125536/3 "2008-01-12T11:20:28Z")

</div>

hiddenhippo wrote:

> ```
> rescue Exception => e
> 
> ```
> 
> thanks
> 
> >

With Rails 2.0 you can now return the status along with the rendered  
text. Something like:  
begin  
Orderitem.destroy(params[:id])  
render :text =\> “order item deleted”  
rescue ActiveRecordError =\> e  
render :text =\> e.message, :status =\> 406  
end

HTH  
Christ

## –

Autopendium :: Stuff about old cars  
[http://autopendium.com](http://autopendium.com)
