Validate in controller or use Rails validations?

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

use them! they encourage proper MVC

(duck behind something solid - I sense a flamewar cometh)

On 8 June 2010 06:36, MarkB [email protected] 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 could do javascript validation and keep the model 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
[email protected]

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