Can you go into more detail on how to do this? I'm new to Ruby on Rails.
> I would do this in the model, after validations. I would use 'each' or
> 'valid?', depending on how you want to handle it.
Saving a record goes through a series of steps in Rails and at each
one of them you can write code in what is called a callback method
(ActiveRecord::Callbacks).
Before Rails tries to save a record validations are run. Those
validations might generate 'errors' (they are not really Ruby errors
since you don't need to "rescue" them, just errors of your model),
which are stored in an object so you can use them in your view.
'each' and 'valid?' are methods that allow you to work with those
errors.
'each' (http://api.rubyonrails.org/classes/ActiveModel/
Errors.html#method-i-each) will allow you to loop through the list of
errors and use that information to clear all fields with errors.
'valid?' (http://api.rubyonrails.org/classes/ActiveRecord/
Validations.html#method-i-valid%3F) allows you to check for a specific
field 'manually'.
The benefit of using 'each' is that it will just do everything by
itself once you have it set up, even if you add/remove columns to your
table. It's just a bit more complicated than using 'valid?' but it
will save you tons of time if you ever change the columns of the table
because you don't have to go back and look in the code to see why the
new fields are not been cleared and "where the heck did I put that
code that clears the fields anyway?", although if you do things "the
right way" this should be a mute point.
You can use the 'after_validation' callback to put your code in a
method:
<code>
after_validation :clear_fields
def clear_fields
# you clear your fields here
end
</code>
> If you clear the field in the model, after validations have run (and
> you have your errors for display in the view), then your fields will
> be already set for your view.
If you do it as I mentioned above, in the model, your validations run
and generate errors (or not). If errors are generated already you have
them so you don't have to worry about 'losing' them so after the
callback code runs the errors will still be available.
I think that clearing the fields in the
> controller or the view is a mistake as it's not their job to manage
> the contents of the model values.
This is a separation of concerns issue. The controller should 'know'
very little about the model. The model should know what to do when
certain things happen to it. Think about it this way and if you have
children you will appreciate better, I think. Wouldn't it be nice that
you didn't have to worry about repeating for the 1,000,000th time to
your X year old kid to go to the bathroom instead of just doing it in
the diaper? Well, by the time that kid is a teenager (hopefully
sooner) he/she doesn't use a diaper anymore. Why? Because you taught
him/her to take care of him/herself. What if you "teach" your models
to take care of themselves? It's not the responsibility of the
controller to babysit the model, the model is an 'adult' and should
know how to take care of itself. The added benefit to this is that
your models are more 'receptive' to your teachings than your kids.
I would only do this in the
> controller if the default behavior is not to have the values cleared;
> for example, if the values should be cleared in one part of your
> application and not in another part of your application.
I think this is covered in the prior paragraph.