I'm trying to strip
the whitespaces of the variable Username
in my User Model.
I'm using
before_save do
self.username.strip!
end
but it doesn't seem to work, am i missing something ?
Method 1: Here, we will be using trim () method. We can strip a string using trim () method and can remove unnecessary trailing and leading spaces and tabs from the string. Method 2: Here we will be making a user-defined function. We will be writing a JavaScript to strip the string and to remove unnecessary spaces.
Definition and Usage The strip () method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)
Python strip () in-built method, which is used to remove all the leading and trailing spaces from a string (beginning and end (both sides) of a string).
The syntax of strip () is: chars (optional) – a string specifying the set of characters to be removed. If the chars argument is not provided, all leading and trailing whitespaces are removed from the string.
You'd rather update the setter instead of polluting your model with callbacks:
def username=(value)
self[:username] = value.to_s.strip
end
Btw, I prefer squish
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With