Before_save, split the line

I am trying to strip spaces of the Username variable in my user model.

I use

 before_save do self.username.strip! end 

but it doesn’t seem to work, am I missing something?

+7
ruby-on-rails ruby-on-rails-4 before-save
source share
2 answers

You would prefer to update the installer instead of polluting your model with callbacks:

 def username=(value) self[:username] = value.to_s.strip end 

Btw, I prefer squish

+17
source share

If you want to remove only the leading and trailing white space, you can use .strip!

But as you said:

I am trying to remove the spaces of the username variable in my user model.

I think that in fact you want to remove all spaces , the following should do:

.gsub(/\s+/, "")

EDIT:

Oh yes, you can also use the built-in Rail squish() method

thanx for apneadiving for reminder

0
source share

All Articles