Do it in a callback in the model.
before_save :strip_username
private
def strip_username
self.username.strip!
end
Question
As part of my validation for a user name I need to strip the white space from the beginning and end of the user input. I know that the .strip method will do this for me but I'm unsure how to use this method.
At what point do I strip the white spaces off a user input? should it be in the form where the info is taken in? Or in the controller or model?
Solution
Do it in a callback in the model.
before_save :strip_username
private
def strip_username
self.username.strip!
end
OTHER TIPS
I'd recommend doing it at the controller level, especially since this is pre-processing logic, something like removing whitespace should be done at the controller level.
Now, if removing whitespace is common and would always need to be done, say, on creating a new record, then a before_filter
on the model would make sense - it all depends on what your form is doing.