Sam Donaldson
2006-Sep-29 09:32 UTC
Rails validation scheme: why not validate params directly.
Hi. There are many ways of doing validation but I''m trying to think of the cleanest way without making the action code unclean. Would it be advisable to do validation on the incoming params hash rather than waiting to setup an object you are about to save and calling ojb.valid? before saving? Or even going as far as trying to save the object but failing from the model''s validation scheme? Handling errors - How do people display errors cleanly? Do they use flash[:notice] in a try/catch block in the action to catch the validation exception? Any help would be appreciated on designing this. I don''t want to mess up the actions with all the possible erorr messages etc.. This jsut makes the code very unreadable. Thanks, Sam. -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---
Paul Lynch
2006-Sep-29 09:49 UTC
Re: Rails validation scheme: why not validate params directly.
On 29 Sep 2006, at 10:32, Sam Donaldson wrote:> Hi. There are many ways of doing validation but I''m trying to > think of > the cleanest way without making the action code unclean. Would it be > advisable to do validation on the incoming params hash rather than > waiting to setup an object you are about to save and calling > ojb.valid? > before saving? Or even going as far as trying to save the object but > failing from the model''s validation scheme?Why validate params when your model knows better how to validate already? The place for most validation is within the model. There is some validation which is better done at the controller level, so this isn''t an absolute rule. If validation fails, the model won''t be saved, so an additional check on valid? isn''t normally required.> Handling errors - How do people display errors cleanly? Do they use > flash[:notice] in a try/catch block in the action to catch the > validation exception?No, just conditionally display the flash within your normal view template, and set it from a check on your save result.> Any help would be appreciated on designing this. I don''t want to mess > up the actions with all the possible erorr messages etc.. This jsut > makes the code very unreadable.You could get very tricky with checks on all sorts of things, but it''s usually better to go with the flow and just let Rails handle it. If you have followed the examples in the AWDwR book, you will have seen that this needn''t result in overcomplex code. Paul --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---