JamesJY
JamesJY

Reputation: 135

Capitalizing Names before creation. Devise

I am trying to get my user model to capitalize all names when a user signs up. It doesn't appear to the working however. I am using devise.

There is a name field in the database.

user model:

before_create :capitalize_name    

def capitalize_name
  name_array = name.split(" ")
  name_array.each { |name| name.capitalize! }
  name = name_array.join(" ")
end

Upvotes: 1

Views: 88

Answers (2)

Danil Speransky
Danil Speransky

Reputation: 30463

The problem is that you assign the result to the local variable name. Use self.name.

self.name = name.split.map(&:capitalize).join(' ')

Upvotes: 3

j-dexx
j-dexx

Reputation: 10416

Try

self.name = name_array.join(" ")

Upvotes: 2

Related Questions