As I prepare to add validations to my application, I am trying to decide if it would be better to write validation logic in the update/ create controller methods or use Rails'' validation methods. In the former case, I would check the field values provided by the user before saving to the database and redirect back to the input URL if a validation fails. The presence of so many built-in validation methods tells me there must be a good reason to use them. Yet reading through the documentation I can''t be sure what the benefit is. I appreciate any guidance from the experts in this forum. Thanks in advance, Mark -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
use them! they encourage proper MVC (duck behind something solid - I sense a flamewar cometh)> As I prepare to add validations to my application, I am trying to > decide if it would be better to write validation logic in the update/ > create controller methods or use Rails'' validation methods. In the > former case, I would check the field values provided by the user > before saving to the database and redirect back to the input URL if a > validation fails. The presence of so many built-in validation methods > tells me there must be a good reason to use them. Yet reading through > the documentation I can''t be sure what the benefit is. I appreciate > any guidance from the experts in this forum. > > Thanks in advance, > Mark-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Michael Pavling
2010-Jun-08 07:04 UTC
Re: Validate in controller or use Rails validations?
On 8 June 2010 06:36, MarkB <mark_balch-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> In the former case, I would check the field values provided by the user > before saving to the database and redirect back to the input URL if a > validation fails.Let''s hope that you remember to check every field for valid values in every controller method you do any DB saves in.... those methods handling associations are gonna become a spaghetti mess...> reading through > the documentation I can''t be sure what the benefit is.In the simplest terms; What data a model needs is its business. If other parts of the application logic are validating models, then you''re tightly coupling your classes together, and that''s not a very good OO design. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
You could do javascript validation and keep the model validations -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Jun-08 11:58 UTC
Re: Validate in controller or use Rails validations?
MarkB wrote:> As I prepare to add validations to my application, I am trying to > decide if it would be better to write validation logic in the update/ > create controller methods or use Rails'' validation methods.Use Rails'' validation methods. Logic like that does not belong in the controller. [...]> The presence of so many built-in validation methods > tells me there must be a good reason to use them. Yet reading through > the documentation I can''t be sure what the benefit is.The benefit is that they keep your controllers skinny! Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks everyone. Makes sense. Better to build validation logic once in the model than try to add redundant code across controllers. I wanted to ask before I started off on a bad path. Mark On Jun 8, 4:58 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> MarkB wrote: > > As I prepare to add validations to my application, I am trying to > > decide if it would be better to write validation logic in the update/ > > create controller methods or use Rails'' validation methods. > > Use Rails'' validation methods. Logic like that does not belong in the > controller. > > [...] > > > The presence of so many built-in validation methods > > tells me there must be a good reason to use them. Yet reading through > > the documentation I can''t be sure what the benefit is. > > The benefit is that they keep your controllers skinny! > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.