سؤال

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?

هل كانت مفيدة؟

المحلول

Do it in a callback in the model.

before_save :strip_username

private
def strip_username
  self.username.strip!
end

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top