arifb
2008-Sep-15 04:46 UTC
what is best practice to indicate exception/error in controller?
Hi all, Just getting familiar with rails and am performing some checks in a method within a controller. If the checks fail, I would like to indicate to the user a custom error message and stop the update (the method is updating a record in the model). What are best practices to do this? I understand that for form validation I should be putting validation checks in the model however this is quite different. Should I be raising an exception? Ideally, I would like to indicate the error to the user in a similar fashion as when a form validation fails. 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 -~----------~----~----~----~------~----~------~--~---
namxam
2008-Sep-15 09:42 UTC
Re: what is best practice to indicate exception/error in controller?
I would say it depends on your coding style. I ignored exceptions for a long time and I am still not sure whether to use them or not (heard that stack traces are quite expensive). But i would say it comes down to a specific coding style. Some people prefer boolean conditions, others exceptions. For simple things I would for an if-condition as it does not break the execution of the application, while exception will stop the current flow and "goto" somewhere else in the code. I use them for global stuff like authentication and permission checks, not for in page stuff. But as a business master this is only what i learned... a professional might have a different opinion on that topic ;) Max On Sep 15, 6:46 am, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, > > Just getting familiar with rails and am performing some checks in a > method within a controller. If the checks fail, I would like to > indicate to the user a custom error message and stop the update (the > method is updating a record in the model). > > What are best practices to do this? I understand that for form > validation I should be putting validation checks in the model however > this is quite different. Should I be raising an exception? > > Ideally, I would like to indicate the error to the user in a similar > fashion as when a form validation fails. > > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---