开发者

Optimal method to process form params before DB storage?

I'd like to process the info in my form params before they enter the database, I just want to know what the optimal method is to do this.

For example, for my users model, should I add a method to each expected param, for example:

开发者_如何学C
def first_name=(name)
  self.first_name = name.capitalize.strip
end

Or should I modify the form params in another way?


Your approach will probably result in a stack overflow. You are calling the first_name= function recursively as every time you set self.first_name it is calling first_name=

The correct way to do this is as follows :-

def first_name=(name)
  write_attribute( :name, name.capitalize.strip)
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜