When I first started learning ruby on rails I wrote my controller actions by using if else statements to display error messages in case something failed a validation. Like: if @user.save flash[:notice] = "User was saved" redirect_to whatever else flash[:error] = "There was an error saving the user" end Im trying to use the beast forum plugin and in the controller actions there are no if else statements and it looks really clean. like it just has: def create @forum.attributes = params[:forum] @forum.save! .... end Im wondering, does it just rely on validations for the error handling? And if so, is this good practice? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David wrote:> When I first started learning ruby on rails I wrote my controller > actions by using if else statements to display error messages in case > something failed a validation. Like: > > if @user.save > flash[:notice] = "User was saved" > redirect_to whatever > else > flash[:error] = "There was an error saving the user" > end > > Im trying to use the beast forum plugin and in the controller actions > there are no if else statements and it looks really clean. like it > just has: > > def create > @forum.attributes = params[:forum] > @forum.save! > .... > end > > Im wondering, does it just rely on validations for the error > handling? And if so, is this good practice?The second one is user-hostile. Roughly speaking, all tests, and some low-level code, should use save!. That raises an error, which you ought to rescue. The first example does not raise, and it does tell the user something went wrong. However... The best version tells the user what model''s .errors collection now holds. The first example may have put the correct code to do that, in the View, under where it probably output the flash[:error]. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
\"Wolas!\"
2009-Mar-16 11:36 UTC
Re: If else statments for errors within controller methods
Also, the redirects when saving/updating a model generally change. If something went wrong, then you (usually) want to redirect to an edit/ mew page whilst if it actually saved it you want to go to the show. Now this is not always true and with (for example) some models you are going to go to the same page in both cases. Nevertheless, the second way (no if...else...) you will have tor escue the exception that can be thrown if the model is invalid. this will have to be done in ApplicationController, and you would need to abstract all as one method (or call a method depending on request.path which is a bad bad idea) so you would have really unusefull error messages like "something went wrong") On Mar 15, 6:58 am, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> David wrote: > > When I first started learning ruby on rails I wrote my controller > > actions by using if else statements to display error messages in case > > something failed a validation. Like: > > > if @user.save > > flash[:notice] = "User was saved" > > redirect_to whatever > > else > > flash[:error] = "There was an error saving the user" > > end > > > Im trying to use the beast forum plugin and in the controller actions > > there are no if else statements and it looks really clean. like it > > just has: > > > def create > > @forum.attributes = params[:forum] > > @forum.save! > > .... > > end > > > Im wondering, does it just rely on validations for the error > > handling? And if so, is this good practice? > > The second one is user-hostile. > > Roughly speaking, all tests, and some low-level code, should use save!. That > raises an error, which you ought to rescue. > > The first example does not raise, and it does tell the user something went > wrong. However... > > The best version tells the user what model''s .errors collection now holds. The > first example may have put the correct code to do that, in the View, under where > it probably output the flash[:error].--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---