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.
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.
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!