开发者

Titlecase a username in ruby before save

Is there an easy way to make sure the username is titlecase before it gets saved in ruby....i was thinking a model method but not 100% sure

so if a user entered apple 开发者_Go百科then i want it to be stored in the db as Apple


Yea i do think a before_save is a good option for this one. I don't know if you have seperated fields for the first/last and middle names, it would be easier then because the middle name should not be capitalized.

Here is an easy implementation:

before_save :capitalize_names

protected
  def capitalize_names
    ['first_name', 'last_name', 'middle_name'].each do |name|
      self.attributes[name] = self.attributes[name].capitalize
    end
  end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜