ajax post raise error

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

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.

hiddenhippo wrote:

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
>

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