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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <redaudi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- ---------------------------- Autopendium :: Stuff about old cars http://autopendium.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---