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 ?
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 ?
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
nil
possibility –
Contumacious super(value.to_s.strip)
instead of self[:attr] = value.to_s.strip
if it's ActiveRecord –
Arcboutant If you want to remove only leading and trailing white space you can use .strip!
But as you said:
I'm trying to strip the whitespaces of the variable Username in my User Model.
I think actually you want to remove all spaces following should do:
.gsub(/\s+/, "")
EDIT:
Oh yes, You can also use Rail's built-in method squish()
thanx to apneadiving for reminding
© 2022 - 2024 — McMap. All rights reserved.
self.username = self.username.strip!
– Boineystrip!
already do that. – Digester