On 29 Sep 2006, at 10:32, Sam D. wrote:
Hi. There are many ways of doing validation but I’m trying to
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
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
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
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.